Chapter 9

9.1 Fill in the blanks in each of the following statements:

  1. Program modules in JavaScript are called functions.
  2. A variable known only inside the function in which it is defined is called a local variable.
  3. The return statement in a called function can be used to pass the value of an expression back to the calling function.
  4. The keyword function indicates the begining of a function definition.

9.2 For the given program, state the scope (either global scope or function scope) of each of the following elements:

  1. The variable x. Global Scope
  2. The variable y. Functional Scope
  3. The function cube. Global Scope
  4. The function output. Global Scope

9.3 Fill in the blanks in each of the following statements:

  1. Programmer-defined functions, global variables and JavaScript's global functions are all part of the global object.
  2. Function isNaN determines if its argument is or is not a number.
  3. Function escape takes a string argument and returns a string in which all spaces, punctuation, accent characters and any other character that is not in the ASCII character set are encoded in a hexadecimal format.
  4. Function eval takes a string argument representing JavaScript code to execute.
  5. Function unescape takes a string as its argument and returns a string in which all characters that were previously encoded with escape are decoded.

9.4 Fill in the blanks in each of the following statements:

  1. An identifier's scope is the portion of the program in which it can be used.
  2. The three ways to return control from a called function to a caller are return, return expression and encountering the closing right brace of a function.
  3. The Math.random function is used to produce random numbers.
  4. Variables declared in a block or in a function's parameter list are of local scope.

9.5 Locate the error in each of the following program segments and explain how to correct it:

  1. Error: Method is not the keyword used to begin a function definition. Correction: Change method to function.
  2. Error: The function is supposed to return a value, but does not. Correction: Either delete variable result and place the statement
       return x + y;
    in the function or add the following statement at the end of the function body:
       return result;.
  3. Error: The semicolon after the right parenthesis that encloses the parameter list. Correction: Delete the semicolon after the right parenthesis of the parameter list.

9.6 Write a complete JavaScript program to prompt the user for the radius of a sphere, and call function sphereVolume to calculate and display the volume of the sphere.

9.7 Write a script that prompts the user for the radius of a circle, uses a function circleArea to calculate the area of a circle, and prints the area of the circle.

9.9 Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be floating-point values. Incorporate this function into a script that enables the user to enter the coordinates of the points through an XHTML form.

9.10 Answer each of the following questions:

  1. What does it mean to choose numbers "at random"?
    "Random" means that all numbers have an equal chance of being selected.
  2. Why is the Math.random function useful for simulating games of chance?
    The function is useful since values have an equal chance of occurring, as in games of chance.
  3. Why is it often necessary to scale and/or shift the values produced by Math.random?
    Shifting or scaling values is necessary when values must fall within a certain range.
  4. Why is computerized simulation of real-world situations a useful technique?
    Computer simulation of real-world situations allows the user to gain informaiton about a situation without the situation actually occurring.

9.21 Write a function perfect that determines whether parameter number is a perfect number. Use this function in a script that determines and displays all the perfect numbers between 1 and 1000.

9.26 Write a script that simulates coin tossing. Let the program toss the coin each time the user clicks the Toss button. Count the number of times each side of the coin appears. Display the results.

9.28 Modify the program in Exercise 9.27 to print one of a variety of comments for each correct answer and each incorrect answer. Use random number generation to chose a number from 1 to 4 that will be used to select an appropriate response to each answer. Use a switch statement to issue the responses.

9.33 What does the following function do?

The function adds a to itself b number of times.

Home