Chapter 6

Question 6.1

  1. //
  2. Semiclion
  3. if
  4. window
  5. keywords
  6. write, writeln, document

Question 6.2

  1. False. Comments do not cause any action to be performed when the script is executed. They’re used to document scripts and improve their readability.
  2. False. JavaScript is case sensitive, so these variables are distinct.
  3. True.
  4. False. The operators *, / and % are on the same level of precedence, and the operators + and - are on a lower level of precedence.
  5. False. Function parseInt converts a string to an integer value.

Question 6.3

  1. var c, thisIsAVariable, q76354, number;
  2. value = window.prompt( "Enter an integer", "0" );
  3. var age = parseInt( stringValu;
  4. if ( number != 7 ) window.alert( "The variable number is not equal to 7" );
  5. document.writeln( "This is JavaScript" );

Question 6.4

  1. 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. [Note: The result of this error is that the output statement is executed whether or not the condition in the if statement is true. The semicolon after the right parenthesis is considered an empty statement a statement that does nothing.]
  2. Error: The relational operator => is incorrect. Correction: Change => to >=

Question 6.5

  1. // Calculate the product of three integers
  2. var x, y, z, result;
  3. var xVal, yVal, zVal;
  4. xVal = window.prompt( "Enter first integer:", "0" );
  5. yVal = window.prompt( "Enter second integer:", "0" );
  6. zVal = window.prompt( "Enter third integer:", "0" );
  7. x = parseInt( xVal );
  8. y = parseInt( yVal );
  9. i) z = parseInt( zVal );
  10. j) result = x * y * z;
  11. k) document.writeln( "<h1>The product is " + result + "</h1>" );

Question 6.6

  1. Using the statements you wrote in Exercise 6.5, write a complete script that calculates and prints the product of three integers.
  2. Calculator

Question 6.7

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

  1. Comments are used to document a script and improve it's readability.
  2. A dialog capable of receiving input from the user is displayed with method prompt of object window
  3. A JavaScript satement that makes a decision is the if statement.
  4. Calculations are normally performed by arithmetic operators.
  5. Method alert of object window displays a dialog with a message to the user.

Question 6.8

Write JavaScript statements that accomplish each of the following tasks:

  1. Display the message "Enter two numbers" using the window object.
  2. window.alert("Enter two numbers");
  3. Assign the product of variables b and c to variable a.
  4. var a, b, c;
    a = b*c
  5. State that a script performs a sample payroll calculation.
  6. // Program performs a sample payroll calculation.

Question 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. False: Operators follow rules of operator procedure.
  2. The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$, his_$account_total, a, b$, c z, z2. True.
  3. A valid JS arithmetic expression with no parentheses is evaluated from left to right. False *,/ or % is evaluated first than + or -
  4. The following are all invalid variable names: 3g, 87, 67h2, h22, 2h. False: h22 is valid.

Question 6.10

Fill in the blanks in each of the following statements:

  1. What arithmetic operations have the same precedence as multiplication? Division and Mod operations
  2. When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? The one inside the nested
  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 variable.

Question 6.11

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

  1. window.alert("x= " + x)x = 2
  2. window.alert("The value of x + x is " + ( x+x) );
  3. The value of x + x is 4
  4. window.alert("x=") x=
  5. window.alert( (x+y) + "=" + (y+x)); 5 = 5

Question 6.12

Which of the following JavaScript statements contain varibles whose values are changed?

  1. p = i + j + k + 7 the p varible has changed
  2. window.alert( "variables whose values are destroyed" ); no change
  3. window.alert( "a = 5" ); no change
  4. stringVal = window.prompt( "Enter string:" )stringVal varible will change to what is entered by the user

Question 6.13

6.13 Given y = ax^3 + 7, which of the following are correct JavaScript statements for this equation?

  1. y = a * x * x * x + 7; correct
  2. y = a * x * x * ( x + 7 ); incorrect
  3. y = ( a * x ) * x * ( x + 7 ); incorrect
  4. y = ( a * x ) * x * x + 7; correct
  5. y = a * ( x * x * x ) + 7; correct
  6. y = a * x * ( x * x + 7 ); incorrect

Question 6.14

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;
  2. x = 2 % 2 + 2 * 2 - 2 / 2;
  3. x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );