Lesson Plan
 
IMOS Home Student Center TallTech Home
 

December 7, 1999

Details

Title

JavaScript Q&A, Closer look at looping

Time Allotment

3 hrs

Reading / References

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

Objectives

Coursework / Lab Work

For Marks

Assignment #: JS01
Assignment: JavaScript Language Syntax
Due: Dec 16/99
Marks: 10 marks

Complete case problems #1 & #3 from pp 7.44 - 7.49

Homework

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.

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>