JavaScript Loops - Homework Questions

Q1

Take the example program that displays all the times tables starting from 1 x 1 = 1 and going up to 12 x 12 = 144 and rewrite it so that it only uses do-while loops.

Q2

The following loop should terminate when the user enters a number which is either less than 10 or greater than 20. However, it contains a few errors. What are the errors and how should the program be rewritten?

<script language="JavaScript">
var number;
var s = prompt("Please enter a number outside the range 10 to 20","");
while s > 10 || s < 20
  { number = parseFloat(s);
  }
document.write("You entered the value " + number + ".<P>");
</SCRIPT>

Q3

Write a program that displays a table containing 10 rows and 10 columns on the screen, with each cell containing the letter X. You will recall that in HTML a table is enclosed within <table> and </table> tags, that each row within that table is enclosed within <tr> and </tr> tags, and that each cell within that row is enclosed within <td> and </td> tags.

Q4

The average of a series of number is found by adding the numbers together and dividing by how many numbers there are (so the average of 2, 5, 1 and 7 is equal to (2 + 5 + 1 + 7) / 4 = 15 / 4 = 3.75). Write a program which asks the user for a series of numbers, to be entered one at a time, and calculates their sum, their average, the smallest value entered and the largest value entered.

Q5

Write a program which gets the user to enter the digits of a number one at a time (e.g. 7 followed by 3 followed by 4) and then constructs the number from them (e.g. 734). The program wouldn't know in advance how many digits there were going to be, so the program would have to indicate a particular stopping condition which the user can enter to indicate that there are no more digits to come.

Those of you who are familiar with the binary system might like to adapt the program so that the user can enter the digits of a binary number one at a time, and the program will display the equivalent base 10 number.

Q6

Mathematics contains a special function called factorial, which is defined as follows:

0! and 1! (called "zero factorial" and "one factorial" respectively) are both defined as being 1.
2! ("two factorial") = 2 x 1 = 2
3! ("three factorial") = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120 etc.

Write a program which asks the user for a positive whole number and calculates the factorial of it. Beware, though! Factorial numbers quickly become very large - for instance, 10! is well over a million!

Q7

The first train to London from my local train station arrives promptly at 5.33 in the morning and then a train arrives regularly every 19 minutes after that. However, the service stops fairly early: anyone arriving at the station after 10 p.m. has missed the last train.

  1. Write a program which displays the times of all the trains to London.

  2. Adapt the program so that it also counts the number of trains to London.

  3. The journey to London takes exactly 34 minutes. Adapt the program so that it also gives the arrival times of all the trains.

Q8

This is one for you mathematical buffs out there! Consider the following equation:

x3 + 2x2 - 3.5 = 0

There is a value of x somewhere between 1 and 2 which makes the equation true. We know this must be true since, when x = 1, x3 + 2x2 - 3.5 comes out as a negative number, but when x = 2, x3 + 2x2 - 3.5 produces a positive number.

Write a program which tracks the value of the value of x that makes the equation true (the root of the equation) using the following steps:

  1. Set the variable lower_limit to the value 1 and the variable upper_limit to the value 2. We know that the root of the equation is somewhere between lower_limit and upper_limit.

  2. Set the variable x to be the number exactly half way between lower_limit and upper_limit.

  3. Test the value of x3 + 2x2 - 3.5 for the value of x. If it comes out as a negative number, then update the value of lower_limit to be the value of x (i.e. we have brought up the lower bound on where the possible value of the root can lie). If it is a positive number, then update the value of upper_limit similarly. Either way, the program will have narrowed the range on where the root could possibly be.

  4. Go back to step (b) in the list, and repeat the process until the value of the root doesn't change noticeably.

Q9

In the thirteenth century, an Italian mathematician, Leonardo of Pisa (writing under the name Fibonacci) produced a famous list of numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, ....

The first two numbers are both 1 and after that each number is found by adding the previous two numbers together. Write a program which calculates the first 200 terms of the Fibonacci series.

Q10

22 (pronounced '2 squared' or '2 to the power of 2') = 2 x 2 = 4
54 (pronounced '5 to the power of 4') = 5 x 5 x 5 x 5 = 625
63 (pronounced '6 cubed' or '6 to the power of 3') = 6 x 6 x 6 = 216

These are powers, and in general, xy or 'x to the power of y' is x multiplied by itself y times (or should that be y-1 times?)

There is, in fact, a function in JavaScript, called pwr() which calculates powers, but I want you to write a program which asks the user for the number to multiply and the power number, and then calculates one number to the power of the other.


Go back Go back