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.
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>
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.
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.
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.
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!
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.
This is one for you mathematical buffs out there! Consider the following equation:
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:
In the thirteenth century, an Italian mathematician, Leonardo of Pisa (writing under the name Fibonacci) produced a famous list of numbers:
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.
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.