Assignment 4

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 semi-colon.
c) The if statement is used to make decisions.
d) Space characters, newline characters and tab characters are known as whitespace.
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 true or 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 are ignored by the computer.
b) JavaScript considers the variables number and NuMbEr to be identical. False: JavaScript is case sensitive.
c) The remainder operator (%) can be used only with numeric operands. True.
d) The arithmetic operators *, /, %, + and - all have the same level of precedence. False: + and - have a lower level of precedence than the others.
e) Method parseInt converts an integer to a string. False: parseInt turns a string to an integer.

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

a) Declare variables c, thisIsAVariable, q76354 and number.
  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 a 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 box.
  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 text 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 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");
correct:
if (c <7)
    window.alert("c is less than 7");
b)
if (c≥7)
    window.alert("c is equal to or greater than 7");
correct:
if (c≥7)
    window.alert("c is equal to or greater than 7");

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

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, read 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 third value, read the value from the user and store it in the variable 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 contained in variables 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( "The product is " +result+ ".");

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

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

a) Comments are used to document a program and improve its readability.
b) A dialog capable of receiving input from the user is displayed with method prompt of object window.
c) A JavaScript statement 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 alertt 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.
    window.alert("Enter two numbers");
b) Assign the product of variables b and c to variable a.
    var a, b, c;
    a = b*c
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: some operators are evaluated from right to left.
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 JS arithmetic expression with no parentheses is evaluated from left to right. True.
d) 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:

a) What arithmetic operations have the same precedence as multiplication? Division and remainder operations
b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? The nested parentheses
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 displays 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 JS statements contain variables whose values are destroyed (i.e. changed or replaced)?

    d) stringVal = window.prompt("Enter string:"); The prompt window value would be replaced by the entered string.

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

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

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

a) x = 7+3*6/2-1;
    3*6/2+7-1 x = 15
b) x = 2%2+2*2-2/2;
    2%2*2/2+2-2 x = 1
c) x = (3*9*(3+(9*3/(3))));
    3/3*9+3*9*3 x = 324

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:
a) Using one document.writeln statement.
b) Using four document.write statements.

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.

6.17 Write a script that asks the user to enter two integers, obtains the numbers from the user
and ouputs 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."

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.

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 (*).

6.21 Modify the program you created in 6.20 by removing the pre tags.

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.

6.27 Write a script that reads an integer and determines and outputs XHTML text that displays
whether it is odd or even.

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.

6.29 Write a script that outputs XHTML text that displays in the XHTML document a checkerboard pattern.

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 rewulting values in an XHTML table format.

Home