Lesson Plan
 
IMOS Home Student Center TallTech Home
 

December 9, 1999

Details

Title

More statements, alerts, prompts, strings, & math.

Time Allotment

3 hrs

Reading / References

Tutorial 7 - Programming with JavaScript  - New Perspectives Manual (NP)

Objectives

Coursework / Lab Work

Homework

Exercises

Using the previous exercises as a reference:

Solutions

EXERCISE: LOOP EXERCISE WITH PROMPTS

<HTML>
<HEAD>
</HEAD>
<BODY>  
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
      
       var InitBal = prompt("What is the initial balance?", 1000);
       var IntRate = prompt("What is the initial interest rate?", 12);
       var Months = prompt("Calculate for how many months?", 12);
 
       document.write("Initial balance: " + InitBal + "<BR>");
       document.write("Interest rate: " + IntRate + "<BR>");
       document.write("Month&nbsp;&nbsp;&nbsp;&nbsp;Balance<BR>");
 
       var AccumBal = InitBal*1; //ensure that it is thinking it is a number
 
       for(i=1; i<=Months; i++)
       {
              AccumBal = AccumBal + (AccumBal * (IntRate/12/100) );
              document.write(i);
              document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
              document.write(AccumBal + "<BR>");
       }
 
       document.write("Final balance is: " + AccumBal);                 
      
// End Hide -->
</SCRIPT>
 
</BODY>
</HTML>

Demo

Alert Boxes

        alert ("A message to the user");

Prompting the user

var myvariable = prompt("Question goes here?", "Default Value");  

External references

         <SCRIPT LANGUAGE="Javascript" SRC="http://www.mydomain.com/myscript.js">
         </SCRIPT>
 

Looping through an array – compatibility?

         for count in myArray
         {
                  write(myArray[count]);
         }
 
Breaking out early

         count = 0;
         while (count < 10)
         {
           if (count == 5)
             break;
 
           write(count + "...");
           count++;
         }

Jumping to the next iteration
        for iteration in myArray
         {
            if (Math.odd(iteration))
              continue;
 
            writeln(myArray[iteration]);
         }
 
Strings
                Concatenation
                String methods
                                i.e. toUpperCase(), toLowerCase
                                changing attributes 

Working with the math functions
                Math functionality / methods
                               i.e. abs - absolute value, min, max, etc.