Lesson Plan
 

March 20, 2000

Details

Title

Closer look at looping
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

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.

Exercises

EXERCISE 1: BALANCE CALCULATOR

Create a script that calculates the compounded monthly interest on a balance for an entire year. You should define variables for the initial balance and the annual interest rate. Set the variables to 1000 and 12 respectively.

You will need to apply a loop that loops 12 times in order to solve this problem. You will want to create an accumulator variable (name it CurrentBal) to calculate the accrued monthly balance.

*Note that the interest rate is for the whole year. You will want to divide it by 12 and then divide by 100 to make it a percentage.

Each iteration of the loop should perform the calculation, then display the results. After the loop has occurred 12 times, the total should be displayed. Your script should have the following output: 

Initial balance: 1000
Interest rate: 12
Month Balance
    1010
    1020.1
    1030.301
    1040.60401
    1051.0100501
    1061.5201506009998
    1072.1353521070098
    1082.85670562808
    1093.6852726843606
10     1104.6221254112042
11     1115.6683466653162
12     1126.8250301319694
Final balance is: 1126.8250301319694

EXERCISE 2: ARRAYS

An array of integers contains both positive and negative numbers. Assume that the negative numbers are undesirable, and we want to get rid of them. Form a NEW array by copying only the positive integers from the original array.

We define and initialize our array of mixed integers:

var Nums = new Array(5,-7,2,9,-22,-57,12,15,83,-45,22,67,-44);

We assign a variable that keeps track of the number of items in the array:

            var Counter = 13;

Finally we define our new array that will contain the positive integers. We also need a counter that will hold the next available slot in the new array:

     var PosNums = new Array();
   
     var PosNumsIndex = 0;

Our Strategy for copying the positive numbers from Nums to PosNums is to sequentially pass through the array Nums, and each time we arrive at a positive number, copy it to the next available slot in PosNums, then increment the PosNumsIndex: 

     for( NumsIndex=0; NumsIndex<Counter; NumsIndex++)
   
         if(Nums[NumsIndex]>=0)
        PosNums[PosNumsIndex++]=Nums[NumsIndex]; 

Write the script based on the above description. Once the new array has been formed, the script should display all elements of the new array.

SUPPLEMENTAL EXERCISES

Using the previous exercises as a reference:

Solutions

EXERCISE 1: BALANCE CALCULATOR

<HTML>
<HEAD>
</HEAD>
<BODY>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
       var InitBal = 1000;
       var IntRate = 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;
 
       for(i=1; i<=12; 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>

EXERCISE 2: ARRAYS

<HTML>
<HEAD>
</HEAD>
<BODY>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
       var Nums = new Array(5,-7,2,9,-22,-57,12,15,83,-45,22,67,-44);
       var Counter = 13;
 
       var PosNums = new Array();
       var PosNumsIndex = 0;
 
       for( NumsIndex=0; NumsIndex<Counter; NumsIndex++)
       {
              if (Nums[NumsIndex] >= 0)
              {
                     PosNums[PosNumsIndex++]=Nums[NumsIndex];
              }
       }
 
       for( NumsIndex=0; NumsIndex<PosNumsIndex; NumsIndex++)
       {
              document.write(PosNums[NumsIndex] + "<BR>");
       }
      
// End Hide -->
</SCRIPT>

</BODY>
</HTML>

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>