7.1 Fill in the blanks in each of the following statements:
a) All programs can be written in terms of three types of control structures: sequence, selection and repetition. b) The if...else double-selection statement is used to execute one action when a condition is true and another action when that condition is false. c) Repeating a set of instructions a specific number of times is called a counter-controlled repetition. d) When it is not known in advance how many times a set of statements will be repeated, a sentinel, signal, flag or dummy value can be used to terminate the repetition.
7.2 Write four JavaScript statements that each add 1 to variable x, which contains a number.
x = x + 1; x += 1; ++x; x==;
7.3 Write JavaScript statements to accomplish each of the following tasks:
7.4 Write a JavaScript statement to accomplish each of the following tasks:
7.5 Combine the statements that you wrote in Exercise 7.4 into a JavaScript program that calculates and prints the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.
7.6 Determine the value of each variable after the calculation is performed. Assume that, when each statement begins executing, all variables have the integer value 5.
7.7 Identify and correct the errors in each of the following segments of code:
7.8 What is wrong with the following while repetition statement? while ( z >= 0) sum += z;
The value of the variable z is never changed in the body of the while statement. If the loop-continuation condition ( z>= 0) is true, an infinite loop is created. z must be decremented so that it eventually becomes less than 0.
7.9 Identify and correct the errors in each of the following segments of code:
7.10 What does the following program print?
7.11 Develop a JavaScript program that will take as input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and output XHTML text that displays the number of miles per gallon obtained for each tankful and prints the combined number of miles per gallon obtained for all tankfuls up to this point. Use prompt dialogs to obtain data from the user.
7.12 Develop a JavaScript program that will determine whether a department-store customer has exceeded the credit limit on a charge account.
7.13 Develop a program that inputs one salesperson's items sold for the last week, calculates the salesperson's earnings and outputs XHTML text that displays the salesperson's earnings.
7.14 Develop a JavaScript program that will determine the gross pay for each of three employees.
7.15 Write a pseudocode program and then a JavaScript program that inputs a series of 10 single-digit numbers as characters, determines the largest of the numbers and outputs a message that displays the largest number.
7.16 Write a JavaScript program that uses looping to print a table of values. Output the results in an XHTML table. Use CSS to center the data in each column.
7.17 Using an approach similar to that in Exercise 7.15, find the two largest values among the 10 digist entered.
7.18 Modify the program in Fig. 7.11 to validate its inputs. For every value input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.
7.19 What does the example program print?
7.20 What does the example program print?
7.21 a b Determine the output for each of the given segments of code when x is 9 and y is 11, and when x is 11 and y is 9.
7.22 a b c d Modify the given code to produce the output shown in each part of the problem.
7.23 Write a script that reads in the size of the side of a square and outputs XHTML text that displays a hollow square of that size constructed of asteriscks.
7.24 Write a script that reads in a five-digit integer and determines whether it is a palindrome.
7.25 Write a script that outputs XHTML text that displays a checkerboard pattern.
7.26 Write a script that outputs XHTML text that keeps displaying in the browser window the multiples of the integer 2. Your loop should not terminate.
The script runs infinately and the page becomes unresponsive.
7.27 Write a program that will encript data transmitted as four-digit integers.
7.28 What does the example program print?
8.1 State whether each of the following is true or false. If false, explain why.
8.2 Write a JavaScript statement or a set of statements to accomplish each of the following tasks:
8.3 Find the error in each of the following code statements, and explain how to correct it:
8.4 Find the error in each of the following segments of code.
8.5 What does the following script do?
8.6 Write a script that finds the smallest of several non-negative integers. Assume that the first value read specifies the number of values to be input from the user.
8.7 Write a script that calculates the product of the odd integers from 1 to 15 then outputs XHTML text that displays the results.
8.8 Modify the compound interest program in Fig. 8.6 to repeat its steps for interest rates of 5, 6, 7, 8, 9 and 10 percent. Use a for statement to vary the interest rate. Use a separate table for each rate.
8.9 Write a script that outputs XHTML to display the given patterns separately, one below the other.
8.10 Write a script that reads five numbers between 1 and 30. For each number read, output XHTML text that displays a line containing the same number of adjacent asterisks.
8.11 Write a script that uses repetition and a switch structure to print the song "The Twelve Days of Christmas."
8.12 Write a script that reads a series of pairs of numbers as follows:
8.13 Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the given statements print? Are the parentheses necessary in each case?
8.14 Modify Exercise 8.9 to combine your code from the four separate triangles of asterisks into a single script that prints all four patterns side by side, making clever use of nested for statements.
8.15 Use De Morgan's Laws to write equivalent expressions for each of the following, then write a program to show that the original expression and the new expression are equivalent in each case:
8.16 Write a script that prints a diamond shape:
8.17 Modify the program in Exercise 8.16 to read an odd number in the range 1 to 19. This number specifies the number of rows in the diamond. The program should then display a diamond shape of the approiate size.
8.18 Describe in general how you would remove any break statement from a loop in a program and replace it with some structured equivalent.
The break condition could be inserted into the FOR loop.
8.19 What does the following script do?