A Balanced Introduction to Computer Science and Programming

David Reed
Creighton University

Copyright © 2004 by Prentice Hall



Chapter 5: Data Types and Expressions
Solutions to odd numbered exercises


EXERCISE 5.1:   Trace the execution of the following JavaScript code and try to predict its behavior. For each assignment, fill in the boxes corresponding to the values of the variables -- even those values that aren't changed. For each write statement, list the output that would be displayed.

num1 = 14;      
   14   
num1
  
num2 = 8 - (3 + 2);      
   14   
num1
  
    3   
num2
document.write("num1 = " + num1 + ", num2 = " + num2 + "<br />");
      OUTPUT: num1 = 14, num2 = 3

num3 = 99;      
   14   
num1
  
    3   
num2
  
   99   
num3
num1 = 99 / 2;      
  49.5  
num1
  
    3   
num2
  
   99   
num3
num2 = 2 * num1;      
  49.5  
num1
  
    99   
num2
  
   99   
num3
num3 = num2 - (num1 + 45.5);      
  49.5  
num1
  
    99   
num2
  
   4   
num3
document.write("num1 = " + num1 + 
               ", num2 = " + num2 + ", num3 = " + num3 + "<br />");
       OUTPUT: num1 = 49.5, num2 = 99, num3 = 4

num2 = num3 + num1;      
  49.5  
num1
  
  53.5  
num2
  
    4   
num3
document.write("num2 = " + num2 + "<br />");
       OUTPUT: num2 = 53.5

Verify your predictions by cut-and-pasting the above code into a Web page (within SCRIPT tags) and loading that page in the browser. You do not need to save or print this page.



EXERCISE 5.3:   What do each of the following expressions evaluate to? Describe the steps involved in each evaluation (as demonstrated in the two examples above). "My favorite number is " + 10 + 24 ==> ("My favorite number is " + 10) + 24 ==> ("My favorite number is " + "10") + 24 ==> "My favorite number is 10" + 24 ==> "My favorite number is 10" + "24" ==> "My favorite number is 1024" "My favorite number is " + (10 + 24) ==> "My favorite number is " + 34 ==> "My favorite number is " + "34" ==> "My favorite number is 34" "My favorite number is " + 10 + "" + 24 ==> ("My favorite number is " + 10) + "" + 24 ==> ("My favorite number is " + "10") + "" + 24 ==> "My favorite number is 10" + "" + 24 ==> ("My favorite number is 10" + "") + 24 ==> "My favorite number is 10" + 24 ==> "My favorite number is 10" + "24" ==> "My favorite number is 1024"

Verify your predictions by displaying the value of each of these expressions in a Web page. You do not need to save or print this page.



EXERCISE 5.5:   Similar to your ftoc.html page, define a page named ctof.html that does the opposite conversion. That is, it should prompt the user for a temperature in degrees Celsius and convert that temperature to Fahrenheit using the formula: tempInFahr = ((9/5) * tempInCelsius) + 32; Once you have completed the page, use it to convert the following temperatures: 0 Celsius 20 Celsius -10 Celsius 88 Celsius

<html> <!-- ctof.html Dave Reed --> <!-- --> <!-- Converts a temperature from Celsius to Fahrenheit.--> <!-------------------------------------------------------> <head> <title>Celsius to Fahrenheit</title> </head> <body> <script type="text/javascript"> tempInCelsius = prompt("Enter the temperature (in Celsius):", "0"); tempInCelsius = parseFloat(tempInCelsius); tempInFahr = ((9/5) * tempInCelsius) + 32; document.write("You entered " + tempInCelsius + " degrees Celsius.<br />"); document.write("That's equivalent to " + tempInFahr + " degrees Fahrenheit."); </script> </body> </html> --------------------------------------- 0 Celsius --> 32 Fahrenheit 20 Celsius --> 68 Fahrenheit -10 Celsius --> 14 Fahrenheit 88 Celsius --> 190.4 Fahrenheit



EXERCISE 5.7:    Experiment with each of the following mathematical functions and identify its purpose. Be sure to test a variety of number values for each function, including negative numbers and fractions. Descriptions of the Math.sqrt and Math.max functions are already provided for you. You do not need to save or print the Web page you use for testing these functions.

FUNCTION INPUTS DESCRIPTION



Math.sqrt 1 number    returns the square root of the input
Math.max 2 numbers    returns the greater of the two inputs
Math.min 2 numbers    returns the lesser of the two inputs
Math.abs 1 number    returns the absolute value of the input
Math.floor 1 number    returns the input rounded down to an integer,
    e.g., Math.floor(2.8) ==> 2
Math.ceil 1 number    returns the input rounded up to an integer,
    e.g., Math.ceil(2.8) ==> 3
Math.round 1 number    returns the input rounded to the nearest integer,
    e.g., Math.round(2.4) ==> 2 and Math.round(2.6) ==> 3


EXERCISE 5.9:    Evaluate each of the following expressions 10 times, and report the results. You do not need to save or print the Web page you use for computing these results. Math.random() ==> random numbers in range [0...1) 2*Math.random() ==> random numbers in range [0...2) 2*Math.random() + 1 ==> random numbers in range [1...3) Math.floor(2*Math.random()+1) ==> random integer in range [1...2] i.e., either 1 or 2