Exercises on the if and switch statementsWith each one of the following tasks, you should write JavaScript programs embedded inside HTML files which test your solutions: Q1Write a program which asks the user for a number between 0 and 100. The program should display one of three messages depending on the value of the number: "Too small", "In range" or "Too large". Q2This program should display the message "That is correct" if the variable a is either 3 or 4. However, it contains several errors. What should the program look like? <script language="JavaScript"> var a = prompt("Enter a number",""); if (a = 3 && a = 4; { document.write("That is correct<P>"); } </script> Q3Look at the following program: <script language="JavaScript"> var first = "Hello", second = "Goodbye"; var number = 32; if (first != "Hello" && second == "Goodbye") { number += 12; number *= 2; } else { number -= 10; } if (first == "Hello" || second != "Goodbye") { number = 13; } else { number *= 5; } alert("The value of number is " + number + "<P>"); </script> What value is displayed on the screen? Don't forget to type the program on the computer to check your answer! Some of the curly brackets in that program aren't really necessary. Rewrite the program removing the unnecessary curly brackets. (N.B. Some of the curly brackets are necessary, so don't remove them all!) Q4Write a program which asks the user to enter the three side lengths of a triangle, one at a time. The program should then check to see if the numbers entered form the sides of a valid triangle. Remember, any two sides of a triangle must add to give a number larger than the third side, and that no side of the triangle can be less than zero. If the program finds a problem with the numbers entered, it should report it to the user. On the other hand, if there is no problem, the program should display the perimeter of the triangle. Q5Write a program that asks the user for a number between 1 and 10. If a suitable number is entered, then the program uses a switch statement to display a message for that number. For instance, if the user enters 3, the program might display "Three blind mice". If the user enters 7, the program might say "Seven days in a week." etc. Q6Write a simple quiz program that asks the user 10 general knowledge questions (such as "What is the capital of France?") and then displays a score at the end. Q7This diagram shows a (very) simple flowchart used to identify an animal that the user has thought of. How would you turn this into a program? You could get the user to type 1 for a Yes answer, and 0 for a No answer, or a character Y or N, which is probably simpler. Q8This is a more complex version of the same thing. You will need to put one of the if statements inside the main part of the first if statement, and another if statement inside the else part (Does that makes sense?) I'm sure you could make it more complicated still. By the use of variables, you avoid having to put IF statements inside others to too great a depth. user_response = prompt("Does the animal have wings? (Enter Y or N),""); if (user_reponse == 'Y') { has_wings = 1; } else { has_wings = 0; } That sort of thing! Q9Now that you have access to IF statements you have all the tools you need to construct a simple calculator program. Try writing a program that gets the user to enter two numbers and then presses a symbol ('+', '-', '*' or '/') to select whether the numbers are to be added, subtracted etc. You can even add other facilities to square, cube numbers etc. Make it as complicated as you like! Q10Adapt the program given as Example 3 in the previous section that calculates a person's body mass index, so that it will specifically inform the user whether he/she is "overweight", "clinically obese" or just "normal". |