Chapter 6

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

a) // begins a single-line comment.

b) Every statement should end with a(n) Semicolon.

c) The if statement is used to make decisions.

d) Space characters, newline characters, and tab characters are known as white space.

e) The window object displays alert dialogs and prompt dialogs.

f) Keywords are words that are reserved for use by JavaScript.

g) Methods write and writeln of the document object write XHTML text into an XHTML document.

6.2 State whether each of the following is trus false. If false, explain why.

a) Comments cause the computer to print the text after the // on the screen when the program is executed. False. Comments do not cause any ction to be performed when the program is executed. They are used to document prgrams and improved their readability.

b) JavaScript considers the variables number and NuMbEr to be identical.False. JavaScript is case sensitive, so these variables are distinct.

c) The remainder operator (%) can be used only with numberic operands. True.

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

e) Method parseInt converts an integer to a string. False. Function parsInt converts a string to an integer value.

6.3

Write JavaScript statements to accomplish each of the following tasks:

a) Declare variables c, thisIsAVariable, q76354 and number. var c, thisIsAVariable, q76354, number;

b) 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" );

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

d) If the variable number is not equal to 7, display "The variable number is not equal to 7" in a message dialog.

if ( number != 7 )

window.alert( "The variable number is not equal to 7" );

e) 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" );

f) Output a line of XHTML texxt that will display the message "This is a JavaScript program" on two lines in the XHTML document. Use only one statement.

document.writeln( "This is a <br/> Javascript program" );

6.4

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

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

b) if( c => 7 )

window.alert( "c is equal to or greater then 7" );

Error: The relational operator => is incorrect.

Correction: Change the => to >=.

6.5

Write a statement ( or comment) to accomplish each of the following task:

a) State that a program will calculate the product of three integers.

//Calculate the product of three integers

b) Declare the variables x, y, z and result.

var x, y, z, result;

c) Declare the variables xVal, yVal and zVal.

var xVal, yVal, zVal;

d) Prompt the user to enter the first value, reas the value from the user and store it in the variable xVal.

xVal = window.prompt( "Enter first integer:", "0" );

e) Prompt the user to enter the second value, read the value from the user and store it in the variable yVal.

yVal = window.prompt( "Enter second integer:", "0" );

f) Prompt the user to enter the the third value, read the value from the user and store it in the vaiable zVal.

zVal = window.prompt( "Enter third integer:", "0" );

g) Convert xVal to an integer, and store the result in the variable x.

x = parseInt( xVal );

h) Convert yVal to an integer, and store the result in the variable y.

y = parseInt( yVal );

i) Convert zVal to an integer, and store the result in the variable z.

z = parseInt( zVal );

j) Compute the product of the three integers containded in cariables x, y and z, and assign the result to the variable result.

result = x * y * z;

k) 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 products of three integers.

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xthml1/DTD/xhtml1-strict.dtd">

<!-- Excercise 6.6: product.html -->

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>

<title>Product of Three Integers</title>

<script type = "text/javascript">

<!--

//Calculate the product of three integers

var x, y, z, result;

var xVal, yVal, zVal;

xVal = window.prompt( "Enter first integer:", "0" );

yVal = window.prompt( "Enter second integer:", "0" );

zVal = window.prompt( "Enter third integer:", "0" );

x = parseInt( xVal );

y = parseInt( yVal );

z = parseInt( zVal );

result = x * y * z;

document.writeln( "<h1>The product is " + result + "</h1>" );

//-->

</script>

</head><body></body>

</html>

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

a)Comments are used to document a program and improve it's readability.

b) A dialog capable of receiving input from the user is displayed with method prompt of object window.

c) A JavaScript satement that makes a decision is the if statement.

d) Calculations are normally performed by arithmetic operators.

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

a) Display the message "Enter two numbers" using the window object.

<script type = "text/javascript">

<!--

window.alert("Enter two numbers");

//-->

</script>

b) Assign the product of variables b and c to variable a.

<script type = "text/javascript">

<!--

var a;

var b;

var c;

a = b + c;

//-->

</script>

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.

a) JavaScript operators are evaluated from left to right. False Operators follow the rules of operator precedence.

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

c) A valid JavaScript arithmetic expression with no parentheses is evaluated from left to right. False evaluated based on the rules of operator precedence.

d) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h. False "h22" was valid. The rest are invalid because they start with numbers.

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

a) What arithmetic operations have the same precedence as multiplication? Division, modulos.

b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? inside then outside

c) 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 diplays in the message dialog when each of the given JavaScript statements is performed? Assume that x=2 and y=3.

a) window.alert( "x = " + x ); x = 2

b) window.alert( "The value of x + x is " + ( x + x )); The value of x + x is 4

c) window.alert( "x = " ); x =

d) window.alert( ( x + y ) + " = " + ( y + x ) ); 5 = 5

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

a) p = i + j + k + 7 this one changes "p" to a new value

b ) window.alert( "variables whose values are destroyed" ); no change

c) window.alert( "a = 5" ); no change

d) stringVal = window.prompt( "Enter string:" ) Replaces var with user returned string

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

a) y = a * x * x * x + 7; correct

b) y = a * x * x * ( x + 7 ); incorrect

c) y = ( a * x ) * x * ( x + 7 ); incorrect

d) y = ( a * x ) * x * x + 7; correct

e) y = a * ( x * x * x ) + 7; correct

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

a) x = 7 + 3 * 6 / 2 - 1; 3*6, 18 / 2, 7 + 9, 16 - 1, x = 15

b) x = 2 % 2 + 2 * 2 - 2 / 2; 2%2, 2*2, 2/2, 0 + 4, 4 - 1, x = 3

c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );3/3, 3+9,3*9,24*12, x = 288

News:

Working on Chapter 15