JavaScript: Control Structure I

Notes

8.11 Drivers are concerned with mileage obtained by their automobiles. One driver has kept track of several tankful of gasoline by recording the number of miles driven and the number of gallons used for each tankful. Develop a JavaScript program that will 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 the data from the user.
Answer

8.12 Develop a JavaScript program that will determine whether a department-store customer has exceded the credit limit on a charge account. For each customer, the following facts are available:

  1. Account number
  2. Balance at the beginning of the month
  3. Total of all items charged by this customer this month
  4. Total of all credits applied to this customer's account this month
  5. Allowed credit limit
The program should input each of these facts from prompt dialogs as integers, calculate the new balance (= beginning balance + charges - credits), display the new balance and determine whether the new balance exceeds the customer's credit limit. For customers whose credit limit is exceeded, the program should output XHTML text that displays the message "Credit limit exceeded."
Answer

8.13 A large company pays its salespeople on a commission basis. The salespeople receive $200 per week, plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week receives $200 plus 9% of $5000, or a total of $650. You have been supplied with a list of items sold by each salesperson. The values of these items are as follows:

Item Value
1 239.99
2 129.75
3 99.95
4 350.89

Develop a program that inputs one salesperson's item sold for last week, calculates the salesperson's earnings and outputs XHTML text that displays the salesperson's earnings.
Answer

8.14 Develop a JavaScript program that will determine the gross pay for each of three employees. The company pays "straight time" for the first 40 hours worked by by each employee and pays "time and a half" for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee, determine the employee's gross pay and output XHTML text that displays the employee's gross pay. Use prompt dialogs to input the data.
Answer

8.15 The process of finding the largest value (i.e. the maximum of a group of values) is used frequently in computer applications. For example, a progam that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. 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 XHTML text that displays the largest number. Your program should use three variables as follows:
a) counter: A counter to count to 10 (i.e. to keep track of how many numbers have been input and to determine when all 10 numbers have been processed);
b) number: The current digit input to the program;
c) largest: The largest number found so far.
Answer

8.16 Write a JavaScript program that uses looping to print the following table of values. Output the results in an XHTML table:

N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000
4 40 400 4000
5 50 500 5000

Answer

8.17 Using an approach similar to that in Exercise 8.15, find the two largest values among the 10 digits entered. [Note: You may input each number only once.]
Answer

8.18 Modify the program in Fig. 8.11 to validate its input. For every value input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.
Answer

8.23 Write a script that reads in the size of a side of a square and outputs XHTML text that displays a hollow square of that size constructed of asterisks. Use a prompt dialog to read the size from the user. Your program should work for squares of all sides between 1 and 20.
Answer

8.24 A palindrome is a number or a text that reads the same backward or forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a script that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, output XHTML text that displays an alert dialog indicating the problem to the user. When the user dismisses the alert dialog, allow the user to enter a new value.
Answer

8.25 Write a script that outputs XHTML text that displays the following chekerboard pattern:
*   *   *   *   *   *   *   *
  *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *
  *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *
  *   *   *   *   *   *   *   *
*   *   *   *   *   *   *   *
  *   *   *   *   *   *   *   *
Your program may use only three output statements, one of the form
		document.write("* ");
one of the form
		document.write(" ");
and one of the form
		document.writeln("<br />");

[Hint: Repetition structures are required in this exercise/]
Answer

8.26 Write a script that outputs XHTML text that keeps displaying in the browser window the multiples of the integer 2, namely 2, 4, 8, 16, 32, 64, etc. Your loop should not terminate (i.e. you should create an infinite loop). What happens when you run this program?
Answer

8.27 A company wants to transmit data over the telephone, but it is concerned that its phones may be tapped. All of its data are transmitted as four-digit integers. It has asked you to write a progam that will encrypt its data so that the data may be transmitted more securely. Your script should read a four-digit integer entered by the user in a prompt dialog and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then output XHTML text that displays the encrypted integer.
Answer

8.28 Write a program that inputs an encrypted four-digit integer (from Exercise 8.27) and decrypts it to form the original number.