BuiltWithNOF
Calc Output

Copy and run this JavaScript program.  Enter a whole number and a simple decimal number for the second.  Look at the output. See if you can follow why the output are these values. Try again.. Now, change the code.  Use different calculations. Again, see if you can predict why the output is what it is.  This type of problem will be on the examination at the end of class.

<html>
   <head>
     <title>Predict the Output</title>

     <script type = "text/javascript">
         <!--
         var firstVal, // These are variable used
           secondVal,  // by the program
           thirdVal;

        
         firstVal = window.prompt( "Enter an Integer", "0" );
         secondVal = window.prompt( "Enter a decimal number", "0" );

         // convert from a string to an integer
         firstVal = parseInt(firstVal);
      
         // convert from a string to a decimal number
         secondVal = parseFloat(secondVal);

         // do some calculations (Change these lines later..)
         thirdVal = firstVal + secondVal;
         firstVal = firstVal + 2;
         secondVal = thirdVal - secondVal;
         thirdVal = secondVal*3;

         // display the results
         document.writeln( "FirstVal is " + firstVal );
         document.writeln( "SecondVal is " + secondVal );
         document.writeln( "ThirdVal is " + thirdVal );
         // -->
     </script>

   </head>
   <body>
     <p>Click Refresh (or Reload) to run the script again</p>
   </body>
</html>

[Home] [Syllabus] [Sessions] [Writing Code] [Help] [Setup] [Instructor]