Chapter 9

9.1

Fill in the blanks in each of the following statments:

a) Program modules in JavaScript are called functions

b) A function is invoked using a function call.

c) A variable known only inside the function in which it is defined is called a local variable.

d) The return statement in a called function can be used to pass the value of an expression back to the calling function.

e) The keyword function indicates the beginning 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:

a) The variable x global

b) The variable y function scope

c) The function cube. global scope

d) The function output.global scope

9.3

Fill in the blanks in eack of the following statements:

a) Programmer-defined functions, globa variables and JavaScript's global functions are all part of the Global object.

b) Function isNAN determines if its argument is or is not a number.

c) 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.

d) Function eval takes a string argument representing JavaScript code to execute.

e) 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:

a) An identifier's scope is the portion of the program in which the it can used.

b) Three ways to return control from a called function to a caller are return; or return expression; or encountering the closing right brace of a function.

c) The Math.random function is used to generate a random number.

d) 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:

a) method g()

{

document.writeln( "Inside method g" );

}

Method is not the word used to define a function. Replace method with function.

b) function sum( x, y )

{

var result;

result = x + y;

}

The program doesn't return the results of the function. Either return x y or return result.

c) function f ( a );

{

document.writeln( a );

}

There shouldn't be a semicolon after the function declaration line. Remove the semicolon.



9.10

Answer each of the following questions:

a) What does it mean to choose numbers "at random". It means that any number has an equal chance of being selected.

b) Why is the Math.random function useful for simulating games of chance? It is useful because you can call on it to generate any number range where any number in the range has an equal or fair chance of coming up in play.

c) Why is it often necessary to scale and/or shift the values produced by Math.random? Because the numbers generated by the function are from 0 < x < 1, and that range may not be useful to the game's requirements.

d) Why is computerized simulation of real-world situations a useful technique? It allows for safe testing of hypothesis and can broaden the possibilities of the real-life situations by allowing for quicker discovery.

9.11

Write statements that assign random integers to the variable n in the following rnages:

a) 1 _< n _< 2 Math.floor( 1 + Math.random() * 2 );

b) 1 _< n _< 100 Math.floor( 1 + Math.random() * 100 );

c) 0 _< n _< 9 Math.floor( 0 + Math.random() * 9 );

d) 1000 _< n _< 1112 Math.floor( 1000 + Math.random() * 1112 );

e) -1 _< n _< 1 Math.floor( -1 + Math.random() * 1 );

f) -3 _< n _< 11 Math.floor( -3 + Math.random() * 11 );

9.12

For each of the following sets of integers, write a single statement that will print a number at random from the set:

a) 2,4,6,8,10 Math.floor( 1 + Math.random() * 5 )*2;

b) 3,5,7,9,11 Math.floor( 1 + Math.random() * 5 )*2+1;

c) 6,10,14,18,22 Math.floor( 1 + Math.random() * 5 )*4+2;

News:

Working on Chapter 15