Chapter 6 Assignments


Home

Section 6.1 Fill in the blanks in each of the following statements:

1. _____begins a single-line comment.
Answer:

2. Every statement should end with a(n)_____.
Answer:

3. The ______ statement is used to make decisions.
Answer:

4. ______, ______, _____and_____ are known as white space.
Answer:

5. The_____ object displays alert dialogs and prompt dialogs.
Answer:

6. _____ are words that are reserved for use by JavaScript.
Answer:

7. Methods_____ and_____ of the_____ object write XHTML text into an XHTML document.
Answer:


Section 6.2 State whether each of the following is true or false. If false, explain why.

1. Comments cause the computer to print the text after the // on the screen when the program is executed.
Answer:

2. JavaScript considers the variables number and NuMbEr to be identical.
Answer:

3. The remainder operator (%) can be used only with numeric operands.
Answer:

4. The arithmetic operators *, /, %, + and - all have the same level of precedence.
Answer:

5. Method parseInt converts an integer to a string.
Answer:


Section 6.3 Write JavaScript statements to accomplish each of the following tasks:

1. Declare variables c, thisIsAVariable, q76354 and number.
Answer:

2. Display a dialog asking the user to enter an integer. Show a default value of 0 in the text field.
Answer:

3. Convert a string to an integer, and store the converted value in variable age. Assume that the string is stored in stringValue.
Answer:

4. If the variable number is not equal to 7, display "The variable number is not equal to 7" in a message dialog.
Answer:

5. Output a line of XHTML text that will display the message "This is a JavaScript program" on one line in the XHTML document.
Answer:

6. Output a line of XHTML text that will display the message "This is a JavaScript program" on two lines in the XHTML document. Use only one statement.
Answer:


Section 6.4 Identify and correct the errors in each of the following statements:

1.

        if ( c < 7 );
          window.alert( "c is less than 7" );
        
Answer:

2.

        if ( c => 7 )
          window.alert( "c is equal to or greater than 7" );
        
Answer:


Section 6.5 Write a statement (or comment) to accomplish each of the following tasks:

1. State that a program will calculate the product of three integers [Hint: Use text that helps to document a program.]
Answer:

2. Declare the variables x, y, z and result.
Answer:

3. Declare the variables xVal, yVal and zVal.
Answer:

4. Prompt the user to enter the first value, read the value from the user and store it in the variable xVal.
Answer:

5. Prompt the user to enter the second value, read the value from the user and store it in the variable yVal.
Answer:

6. Prompt the user to enter the third value, read the value from the user and store it in the variable zVal.
Answer:

7. Convert xVal to an integer, and store the result in the variable x.
Answer:

8. Convert yVal to an integer, and store the result in the variable y.
Answer:

9. Convert zVal to an integer, and store the result in the variable z.
Answer:

10. Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.
Answer:

11. Write a line of XHTML text containing the string "The product is " followed by the value of the variable result.
Answer:


Section 6.6 Perform the following:

Using the statements you wrote in Exercise 6.5, write a complete program that calculates and prints the product of three integers.
Answer:


Section 6.7 Fill in the blanks in each of the following statements:

1. ________are used to document a program and improve its readability.
Answer:

2. A dialog capable of receiving input from the user is displayed with method ________ of object ________.
Answer:

3. A JavaScript statement that makes a decision is the ________ statement.
Answer:

4. Calculations are normally performed by ________ operators.
Answer:

5. A dialog capable of showing a message to the user is displayed with method ________ of object ________.
Answer:


Section 6.8 Write JavaScript statements that accomplish each of the following tasks:

1. Display the message "Enter two numbers" using the window object.
Answer:

2. Assign the product of variables b and c to variable a.
Answer:

3. State that a program performs a sample payroll calculation.
Answer:


Section 6.9 State whether each of the following is true or false. If false, explain why.

1. JavaScript operators are evaluated from left to right.
Answer:

2. The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$, his_$account_total, a, b$, c, z, z2.
Answer:

3. A valid JavaScript arithmetic expression with no parentheses is evaluated from left to right.
Answer:

4. The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.
Answer:


Section 6.10 Fill in the blanks in each of the following statements:

1. What arithmetic operations have the same precedence as multiplication? ______.
Answer:

2. When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? ______.
Answer:

3. A location in the computer’s memory that may contain different values at various times throughout the execution of a program is called a ______.
Answer:


Section 6.11 What displays in the message dialog when each of the given JavaScript statements is performed? Assume that x = 2 and y = 3.

1. window.alert( "x = " + x );
Answer:

2. window.alert( "The value of x + x is " + ( x + x ) );
Answer:

3. window.alert( "x =" );
Answer:

4. window.alert( ( x + y ) + " = " + ( y + x ) );
Answer:


Section 6.12 Which of the following JavaScript statements contain variables whose values are destroyed (i.e., changed or replaced)?

1. p = i + j + k + 7;
Answer:

2. window.alert( "variables whose values are destroyed" );
Answer:

3. window.alert( "a = 5" );
Answer:

4. stringVal = window.prompt( "Enter string:" );
Answer:


Section 6.13 Given y = ax3+ 7, which of the following are correct JavaScript statements for this equation?

1. y = a * x * x * x + 7;
Answer:

2. y = a * x * x * (x + 7);
Answer:

3. y = (a * x) * x * (x + 7);
Answer:

4. y = (a * x) * x * x + 7;
Answer:

5. y = a * (x * x * x) + 7;
Answer:

6. y = a * x * (x * x + 7);
Answer:


Section 6.14 State the order of evaluation of the operators in each of the following JavaScript statements, and show the value of x after each statement is performed.

1. x = 7 + 3 * 6 / 2 - 1;
Answer:

2. x = 2 % 2 + 2 * 2 - 2 / 2;
Answer:

3. x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
Answer:


Section 6.15 Write a script that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the program using the following methods:

1. Using one document.writeln statement.
Answer:

2. Using four document.write statements.
Answer:


Section 6.16

Write a script that asks the user to enter two numbers, obtains the two numbers from the user and outputs text that displays the sum, product, difference and quotient of the two numbers. Use the techniques shown in Fig. 6.9.
Answer:


Section 6.17

Write a script that asks the user to enter two integers, obtains the numbers from the user and outputs text that displays the larger number followed by the words “is larger” in an alert dialog. If the numbers are equal, output XHTML text that displays the message “These numbers are equal.” Use the techniques shown in Fig. 6.17.
Answer:


Section 6.18

Write a script that takes three integers from the user and displays the sum, average, product, smallest and largest of the numbers in an alert dialog.
Answer:


Section 6.19

Write a script that gets from the user the radius of a circle and outputs XHTML text that displays the circle’s diameter, circumference and area. Use the constant value 3.14159 for π. Use the GUI techniques shown in Fig. 6.9. [Note: You may also use the predefined constant Math.PI for the value of π. This constant is more precise than the value 3.14159. The Math object is defined by JavaScript and provides many common mathematical capabilities.] Use the following formulas (r is the radius): diameter = 2r, circumference = 2πr, area = πr 2.
Answer:


Section 6.20

Write a script that outputs XHTML text that displays in the XHTML document a rectangle, an oval, an arrow and a diamond using asterisks (*), as follows [Note: Use the <pre> and </pre> tags to specify that the asterisks should be displayed using a fixed-width font]:

            *********          ***              *              *
            *       *        *     *           ***            * *
            *       *       *       *         *****          *   *
            *       *       *       *           *           *     *
            *       *       *       *           *          *       *
            *       *       *       *           *           *     *
            *       *       *       *           *            *   *
            *       *        *     *            *             * *
            *********          ***              *              *
        
Answer:


Section 6.21

Modify the program you created in Exercise 6.20 by removing the

 and 
tags. Does the program display the shapes exactly as in Exercise 6.20?
Answer:


Section 6.22 What does the following code print?

document.writeln( "*\n**\n***\n****\n*****" );
Answer:


Section 6.23 What does the following code print?

document.writeln( "*" );
document.writeln( "***" );
document.writeln( "*****" );
document.writeln( "****" );
document.writeln( "**" );
Answer:


Section 6.24 What does the following code print?

document.write( "*<br />" );
document.write( "***<br />" );
document.write( "*****<br />" );
document.write( "****<br />" );
document.writeln( "**" );<br /> Answer:


Section 6.25 What does the following code print?

document.write( "*<br />" );
document.writeln( "***" );
document.writeln( "*****" );
document.write( "****<br />" );
document.writeln( "**" );
Answer:


Section 6.26

Write a script that reads five integers and determines and outputs XHTML text that displays the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter.
Answer:


Section 6.27

Write a script that reads an integer and determines and outputs XHTML text that displays whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2 leaves a remainder of zero when divided by 2.]
Answer:


Section 6.28

Write a script that reads in two integers and determines and outputs XHTML text that displays whether the first is a multiple of the second. [Hint:Use the remainder operator.]
Answer:


Section 6.29 Write a script that outputs XHTML text that displays in the XHTML document a checkerboard pattern, as follows:

            * * * * * * * *
             * * * * * * * *
            * * * * * * * *
             * * * * * * * *
            * * * * * * * *
             * * * * * * * *
            * * * * * * * *
             * * * * * * * *
        
Answer:


Section 6.30

Write a script that inputs five numbers and determines and outputs XHTML text that displays the number of negative numbers input, the number of positive numbers input and the number of zeros input.
Answer:


Section 6.31

Write a script that calculates the squares and cubes of the numbers from 0 to 10 and outputs XHTML text that displays the resulting values in an XHTML table format, as follows:

            number  square  cube
            0       0       0
            1       1       1
            2       4       8
            3       9       27
            4       16      64
            5       25      125
            6       36      216
            7       49      343
            8       64      512
            9       81      729
            10      100     1000
            [Note: This program does not require any input from the user.]
        
Answer: