6.1 Fill in the blanks in each of the following statements:
- // begins a single-line comment.
- Every statement should end with a(n) semicolon.
- The if statement is used to make decisions.
- Space characters, newline characters, and tab characters are known as white space.
- The window object displays alert dialogs and prompt dialogs.
- Keywords are words that are reserved for use by JavaScript.
- Methods write and writeln of the document object write XHTML text into an XHTML document.
6.2 State whether each of the following is true or false. If false, explain why.
- Comments cause the computer to print the text after the // on the screen when the program is executed. False. Comments do not cause any action to be performed when the program is executed. They are used to document programs and improve their readability.
- JavaScript considers the variables number and NuMbEr to be identical. False. JavaScript is case sensitive, so these variables are distinct.
- The remainder operator (%) can be used only with numeric operands. True.
- The arithmetic operators *, /, %, + and - all have the same level of precedence. False. The operators *, / and % are on the same level of precedence, and the operators + and - are on a lower level of precedence.
- Method parseInt converts an integer to a string. False. Function parseInt converts a string to an integer value.
6.3 Write JavaScript statements to accomplish each of the following tasks:
- Declare variables c, thisIsAVariable, q76354 and number.
var c, thisIsAVariable, q76354, number; - Display a dialog asking the user to enter an integer. Show a default value of 0 in the text field.
value = window.prompt( "Enter an integer", "0" ); - Convert a string to an integer, and store the converted value in variable age. Assume that the string is stored in stringValue.
var age = parseInt( stringValue ); - If the variable number is not equal to 7, display "The variable number is not equal to 7" in the message dialog.
if ( number !=7 )
window.alert( "The variable number is not equal to 7" ); - Output a line of XHTML text that will display the message "This is a JavaScript program" on one line in the XHTML document.
document.writeln( "This is a JavaScript program" ); - 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.
document.writeln( "The is a <br /> JavaScript program" );
6.4 Identify and correct the errors in each of the following statements:
- if ( c < 7 );
window.alert( "c is less than 7" );
Error: There should not be a semicolon after the right parenthesis of the condition in the if statement. Correction: Remove the semicolon after the right parenthesis. - if ( c => 7 )
window.alert( "c is equal to or greater than 7" );
Error: The relational operator => is incorrect. Correction: Change => to >=.
6.5 Write a statement (or comment) to accomplish each of the following tasks:
- State that a program will calculate the product of three integers [Hint: Use text that helps to document a program.]
// Calculate the product of three integers - Declare the variables x, y, z and result.
var x, y, z, result; - Declare the variables xVal, yVal and zVal.
var xVal, yVal, zVal; - Prompt the user to enter the first value, read the value from the user and store it in the variable xVal.
xVal = window.prompt( "Enter first integer:", "0" ); - Prompt the user to enter the first value, read the value from the user and store it in the variable yVal.
yVal = window.prompt( "Enter second integer:", "0" ); - Prompt the user to enter the first value, read the value from the user and store it in the variable zVal.
zVal = window.prompt( "Enter third integer:", "0" ); - Convert xVal to an integer, and store the result in the variable x.
x = parseInt( xVal ); - Convert yVal to an integer, and store the result in the variable y.
y = parseInt( yVal ); - Convert zVal to an integer, and store the result in the variable z.
z = parseInt( zVal ); - Compute the product of the three integers contained in variables x, y, and z, and assign the result to the variable result.
result = x * y * z; - Write a line of XHTML text containing the string "The product is " followed by the value of the variable result.
document.writeln( "<h1>The product is " + result + "</h1>" );
6.6 Using the statements you wrote in Exercise 6.5, write a complete program that calculates and prints the product of the three integers.
6.7 Fill in the blanks in each of the following statements:
- Comments are used to document a program and improve its readability.
- A dialog capable of receiving input from the user is displayed with method prompt of object window.
- A JavaScript statement that makes a decision is the if statement.
- Calculations are normally performed by binary operators.
- A dialog capable of showing a message to the user is displayed with method alert of object window.
6.8 Write JavaScript statements that accomplish each of the following tasks:
- Display the message "Enter two numbers" using the window object.
window.prompt( "Enter two numbers" ); - Assign the product of variables b and c to variable a.
a = b * c; - State that a program performs a sample payroll calculation.
// This program performs a sample payroll calculation
6.9 State whether each of the following is true or false. If false, explain why.
- JavaScript operators are evaluated from left to right. False. Operator precedence is used, operators of the same precendence level are evaluated left to right.
- The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$, his_$account_total, a, b$, c, z, z2. True.
- A valid JavaScript arithmetic expression with no parentheses is evaluated from left to right. False. Operator precedence is used, operators of the same precendence level are evaluated left to right.
- The following are all invalid variable names: 3g, 87, 67h2, h22, 2h. False. h22 is a valid variable name.
6.10 Fill in the blanks in each of the following statements:
- What arithmetic operations have the same precedence as multiplication? division and remainder
- When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? the inner-most nested
- A location in the computer's memory that may contain different values at various times throughout the execution of a program is called a variable.
6.11 What displays in the message dialog when each of the given JavaScript statements is performed? Assume that x=2 and y=3.
- window.alert( "x = " + x); x = 2
- window.alert( "The value of x + x is " + ( x + x ) ); The value of x + x is 4
- window.alert( "x =" ); x =
- window.alert( ( x + y ) + " = " + ( y + x ) ); 5 = 5
6.12 Which of the following JavaScript statements contain variables whose values are destroyed (i.e., changed or replaced)?
- p = i + j + k + 7; Yes.
- window.alert( "variables whose values are destroyed" ); No.
- window.alert( "a = 5"); No.
- stringVal = window.prompt( "Enter string:" ); Yes.
6.13 Given y = ax3 + 7, which of the following are correct JavaScript statements for this equation?
- y = a * x * x * x + 7; Correct
- y = a * x * x * (x + 7); Incorrect
- y = (a * x) * x * (x + 7); Incorrect
- y = (a * x) * x * x + 7; Correct
- y = a * (x * x * x) + 7; Correct
- y = a * x * (x * x + 7); Incorrect
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.
- x = 7 + 3 * 6 / 2 - 1; multiplication, division, addition, subtraction, assignment, x = 15
- x = 2 % 2 + 2 * 2 - 2 / 2; remainder, multiplication, division, addition, subtraction, assignment, x = 3
- x = ( 3 * 9 ( 3 + ( 9 * 3 / ( 3 ) ) ) ); multiplication, division, addition, multiplication, multiplication, assignment, x = 324
6.15 Write a script that displays the numbers 1 to 4 on the same line, with each pair of numbers separated by one space. Write the program using the following methods:
- Using one document.writeln statement.
document.writeln( "12 34"); - Using four document.write statements.
document.write( "1" );
document.write( "2" );
document.write( " 3" );
document.write( "4" );
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.
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 technigues shown in Fig. 6.17.
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.
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 = πr2.
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]:
6.21 Modify the program you created in Exercise 6.20 by removing the <pre> and </pre> tags. Does the program display the shapes exactly as in Exercise 6.20?
6.22 What does the following code print?
6.23 What does the following code print?
6.24 What does the following code print?
6.25 What does the following code print?
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.
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.]
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.]
6.29 Write a script that outputs XHTML text that displays in the XHTML document a checkerboard pattern, as follows:
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.
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:
Note: This program does not require any input from the user.