Xcreating An Interactive Mathematics Quiz: (For Chapters 12 To 14)
Xcreating An Interactive Mathematics Quiz: (For Chapters 12 To 14)
Chapters 12 to 14)
Introduction
In this worksheet, we are going to create an interactive mathematics quiz using JavaScript.
Basic knowledge of JavaScript is required it has been covered in Chapter 12 to 14 of the
Elective C2 textbook. For further details of JavaScript, visit the web site
http://www.w3schools.com/js/default.asp.
Learning Objectives
After completing this activity, you will be able to
⮚ make use of JavaScript to create an interactive mathematics quiz with marking function
⮚ learn the JavaScript methods random(), floor(x) and the parseFloat function
num1
ans
The functions of the text boxes are shown in the following table:
Text box Function
num1 Store a random integer between 1 and 9 generated by the computer.
op Store one of the operators from ‘+’, ‘-’ and ‘*’ generated by the
computer.
num2 Store a random integer between 1 and 9 generated by the computer.
ans Store the answer inputted by the user.
1. Complete the following HTML codes and input them in a text editor such as Windows
Notepad. Save the file as quiza.html.
Input the HTML codes in Notepad. Select ‘File’ ⮚ ‘Save As’ and save the file as
quiza.html. Remember to select ‘All Files’ as
the file type.
2. Further modify the following HTML codes such that the user cannot change the generated
contents of the text boxes ‘num1’, ‘op’ and ‘num2’.
Method Description
random() Return a random number between 0 and 1.
floor(x) Return x, rounded downwards to the nearest integer.
1. Construct an expression such that it returns an integer between 1 and 10 inclusively. This
expression will be used for generating the numbers in the text boxes ‘num1’ and ‘num2’.
Random()*10+1
3. Include the above JavaScript codes in the HTML codes in Task 1. Test the web page in a
browser to see whether the random integers and operator can be successfully generated.
Several examples are shown as follows.
1. Complete the following checkans() function which checks the answer inputted by the user.
A message will be displayed in an alert box when the ‘Check’ button is clicked as follows.
function checkans() {
var n1 = parseFloat (myform.num1.value
;
var n2 = parseFloat (myform.num2.value
;
var operator = myform.op.value
;
var answer;
if (operator == "+" ) {
answer = n1+n2 ;
}
else if (operator == "-") {
answer = n1-n2 ;
}
else {
answer = n1*n2 ;
}
if ( answer==myform.ans.value
) {
alert("You are correct!")
}
else {
alert("Oops! You are wrong!")
}
}
2. Include the above JavaScript codes in the HTML codes in Task 2. Test the web page in a
browser to see whether the quiz works.
</script>
<body onload="gennum()">
Maths Quiz B
<p>
<form name="myform">
<input type="text" name= "num1" size="2" readonly="readonly">
<input type="text" name= "op" size="2" readonly="readonly">
<input type="text" name= "num2" size="2" readonly="readonly">
=
<input type="text" name= "ans" size="2">
<p>
Your Score:
<input type="button" value="Check" onclick ="checkans();gennum();">
</form>
</body>