Lesson Plan
 
IMOS Home Student Center TallTech Home
 

December 2, 1999

Details

Title

Work with arrays, program loops, and conditional statements

Time Allotment

3 hrs

Reading / References

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

Objectives

Coursework / Lab Work

Homework

Demo

Exercises

EXERCISE 1: UPDATED WEIGHT CONVERTER

Create a weight converter as from last class. This converter should be able to handle conversions either direction. A variable should be passed as the parameter that will allow the decision of which way to convert. Use a string variable as the parameter: Direction = "K" indicates conversion from Kilos to Lbs, Direction = "L" indicates conversion from Lbs to Kilos.

Use the same tests from last class to test your new function.

EXERCISE 2: RECORD RAINFALL

The average high and average low rainfall for July is 24mm and 11mm respectively. Write a program that allows the user to enter the rainfall for July. The program then determines if the rainfall was within the average rainfall range.

Define three variables:

    Var RainFall
    Var MaxRain = 24
    Var MinRain = 11

Assign a value to RainFall to a number of your choice.

Test the value of rainfall against the minimum and maximum rainfall. There is actually two tests to make: is RainFall greater than or equal to min? And is Rainfall less than or equal to Max? Here is how we would form this complex logical expression:

    RainFall>=MinRain && RainFall<=MaxRain

Write the conditional statements that will display either "The amount of rainfall is within normal" or "The amount of rainfall is not within normal" based on the numbers.

EXERCISE 3: ENHANCED DATE DISPLAY

Revise the date display function created last class so it will display the date in the following format:

                Today is Month Day, Year

The "Month" should be the actual name of the month.

Use nested if statements similar to:

                if(NumMonth == 1)
   
         {
   
             StringMonth = "January";
   
         }
   
         else
   
         {
       
if (NumMonth == 2)
   
             {
   
                 StringMonth = "February";
   
             }
   
             else
   
             {
   
                 // continue with rest of months.

Solutions

EXERCISE 1: UPDATED WEIGHT CONVERTER

<HTML>
<HEAD>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
      function ConvertWeight(amount, units)
      {
            if(units == "K")
            {
                  //kilos to pounds
                  var lb = amount * 2.2;
                  return lb;
            }
            else
            {
                  //pounds to kilos
                  var kg = amount / 2.2;
                  return kg;
            }
      }
 
// End Hide -->
</SCRIPT>
 
</HEAD>
<BODY>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
      var kgs = 45;
      var pounds = ConvertWeight(kgs, "K");
      document.write(kgs + " kilograms = " + pounds + " pounds.<BR>");
 
      pounds = 230;
      kgs = ConvertWeight(pounds, "L");
      document.write(pounds + " pounds = " + kgs + " kilograms.<BR>");
 
// End Hide -->
</SCRIPT>
 
</BODY>
</HTML>

EXERCISE 2: RECORD RAINFALL

<HTML>
<HEAD>
</HEAD>
<BODY>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
       var RainFall;
       var MaxRain = 24
       var MinRain = 11
 
       RainFall = 15;
 
       if((RainFall >= MinRain)&&(RainFall <= MaxRain))
       {
              document.write("The amount of rainfall is within normal");
       }
       else
       {
              document.write("The amount of rainfall is not within normal");
       }
// End Hide -->
</SCRIPT>
 
</BODY>
</HTML>

EXERCISE 3: ENHANCED DATE DISPLAY

<HTML>
<HEAD>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
       function DisplayDate(date)
       {
              var NumMonth = date.getMonth() + 1;
              var Day = date.getDate();
              var Year = date.getYear();
 
              if (NumMonth == 1)
              {
                     StringMonth = "January";
              }
              else if (NumMonth == 2)
              {
                     StringMonth = "February";
              }
              else if (NumMonth == 3)
              {
                     StringMonth = "March";
              }
              else if (NumMonth == 4)
              {
                     StringMonth = "April";
              }
              else if (NumMonth == 5)
              {
                     StringMonth = "May";
              }
              else if (NumMonth == 6)
              {
                     StringMonth = "June";
              }
              else if (NumMonth == 7)
              {
                     StringMonth = "July";
              }
              else if (NumMonth == 8)
              {
                     StringMonth = "August";
              }
              else if (NumMonth == 9)
              {
                     StringMonth = "September";
              }
              else if (NumMonth == 10)
              {
                     StringMonth = "October";
              }
              else if (NumMonth == 11)
              {
                     StringMonth = "November";
              }
              else
              {
                     StringMonth = "December";
              }
             
              document.write(StringMonth + " " + Day + ", " + Year + "<BR>");
       }
// End Hide -->
</SCRIPT>
</HEAD>
<BODY>
 
<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from older browsers
       var Today = new Date();
       var Christmas = new Date("December, 25, 1999");
      
       DisplayDate(Today);
       DisplayDate(Christmas);
// End Hide -->
</SCRIPT>
</BODY>
</HTML>