Questions for JavaScript Lesson 2

  1. The following program is riddled with errors. Can you find them and correct them all? When you run the program, what number should it display?

    <script language="JavaScript>
    x = prompt("Please enter a number");
    answer = x % 72
    alert("The answer is " answer)
    <script>
    

  2. Rewrite the following program so that it uses alert() statements:

    <script>
    document.write("There was an old woman ");
    document.write("who lived in a shoe.<p />");
    document.write("She had so many children, ");
    document.write("she didn't know what to do.<p />");
    document.write("But then they grew up ");
    document.write("and she had no-one to handle.<p />");
    document.write("So the old woman moved out ...<p />");
    document.write("... and into a sandal!");
    </script>
    

  3. Write a program that asks for two numbers from the user (which can be whole numbers or decimals!) and displays the sum (adding the numbers), the difference (subtracting the second number from the first), the product (multiplying the numbers), the quotient (dividing the first number by the second) and the remainder of the division that has just been carried out.

  4. Write a program which asks the user for a number and then displays the times table for that number from 1 x to 12 x (i.e. if the user enters the number 7, then the program would display 12 lines, the first one being 1 x 7 = 7 and the last being 12 x 7 = 84). There is an efficient way of doing this using a loop, but you're not supposed to know about those yet, so you will have to put in 12 display statements.

  5. What is the value displayed by the following program?

    <script language="JavaScript">
    var x = 2;
    // x = 7;
    alert(x);
    </script>
    
    The number displayed is

  6. Weighing scales In Britain, we generally measure people's weight in stone and pounds (weight, not money), where 1 stone is equal to 14 pounds. Furthermore, 1 pound is equal to 0.454 kilogrammes (0.454 kg), so a weight in pounds can be converted into kilogrammes by multiplying it by 0.454.
    Stone and pounds (No, not that sort of pounds! Pounds weight!)

    Write a program that uses prompt() twice, once to enter the weight in stone and then again to enter the remaining pounds (i.e. to enter a weight of 14 stone and 12 pounds, '14' would be entered the first time, and '3' the second time), and then displays the weight in kilogrammes.

    I am grossly overweight, and as part of a strict diet I weigh myself once a week. I have two weighing scales - one weighs in stone and pounds and the other in kilogrammes. Unfortunately, I don't know which of the two is more accurate, and I have no way of calibrating them, so I weigh myself on each and then take an average of the two weights. Write a version of your previous program that allows me to enter both weights and then returns the average weight in kilogrammes.


Back to lesson 2 Back to lesson 2