Quiz Exercises
Quiz Exercises
In order to build up you understanding of the javascript code, try these questions, and add some
of your own. Feel free to refer to the text. Keep in mind that many of these are tough questions,
intended to help generate discussions, so don’t worry if you are not sure how to answer them yet.
1. Consider the first call to the handleQuestion function, on line 57 of the code.
a. For each variable below, write what is its value are after line 8 of the handleQuestion code has
executed.
- number
- text
- correctAnswerNumber
- answers
- questionScore
b. Which of these variables store integers? Which store strings (i.e. text)? Which stores an array
of strings?
f. How would you change the function code so that the view would be as in (i) above?
g. How would you change the function so that the view would be as in (ii) above?
2. Suppose that the quiz is to be scored so that –1, rather than 0, is returned when the respondent
gives an incorrect answer. How would you change the code to do this?
3. Find one example of the following in the quiz_results code: an assignment statement, a control
flow statement, a variable declaration statement, a variable, a parameter.
Which variable is an array variable?
4. Do you think that the code would behave differently if we changed the variable name
userAnswerNumber everywhere to, say, Vancouver?
5. How might the code be changed to handle a quiz where there may be more than one correct
answer, and the user need only select one of these?
6. Suppose the user answers 2 to the first question and 4 to the second question. Can you trace
through the order in which the code is executed in quiz_results, and track how the data values
change? (It is very tedious in this case to write down all the lines of code in the order that they
are executed, but you should be able to do this in principle.)
7. Some quizzes, like personality quizzes, assign a score to each question and the feedback at the
end depends on the total score. How might you design such a quiz?
8. The code is organized so that data about each question are passed as parameters to the
handleQuestion function. Another way to organize the code would be to create data about all
questions in the handleQuestion function itself. How might this be done? Which organization
makes most sense to you?
quiz.html
1. <html>
2. <head>
3. <title>CPSC101/WMST201 JavaScript Lab Sample Quiz</title>
4. </head>
5. <body>
9. <tr>
10. <td valign=top height="25"><strong>2.</strong></td>
11. <td colspan=4 height="25">What was the first computer bug? </td>
12. </tr>
13. <tr>
14. <td height="24"></td>
15. <td height="24"><input type=radio name=1 value=0> A COBOL error</td>
16. <td height="24"><input type=radio name=1 value=1> A fruit fly</td>
17. <td height="24"><input type=radio name=1 value=2> A moth</td>
18. <td height="24"><input type=radio name=1 value=3> An overheated vacuum tube</td>
19. </tr>
20. <tr>
21. <td valign=top height="25"><strong>1.</strong></td>
22. <td colspan=4 height="25">What is HTML? </td>
23. </tr>
24. <tr>
25. <td height="24"></td>
26. <td height="24"><input type=radio name=0 value=0> </td>
27. <td height="24"><input type=radio name=0 value=1> HyperText Markup
28. Language</td>
29. <td height="24"><input type=radio name=0 value=2>High Tech Manufacture
30. Ltd.</td>
31. <td height="24"><input type=radio name=0 value=3> </td>
32. </tr>
33. <tr>
34. <td height="28"></td>
35. <td colspan=3 height="28"><input type=submit value="Hand it in!"
name=submit></td>
36. </tr>
37. </table>
38. </form>
39. <hr>
40. </body>
41. </html>
quiz_result.html; (2 pages)
1. <html>
2. <head>
3. <title>quiz results</title>
4. <script language="JavaScript1.1">
5. <!--
6. var lString = document.location + "";
/* comment: lines 7—18 set variable userAnswerNumber to the answer chosen by the
user of the quiz. You can ignore the details. */
7. function handleQuestion(number, text, correctAnswerNumber, answers) {
8. var i, j, userAnswerNumber, questionScore = 0;
37. //-->
38. </script>
39. </head>
40. <body>
41. <h1 align=center>Quiz Results</h1>
42. <script language="JavaScript1.1">
43. <!--
44. var i, answers = new Array("(unanswered)"), score = 0, correct=0;
45. correct = 3;
46. answers[1] = "A COBOL error";
47. answers[2] = "A fruit fly";
48. answers[3] = "A moth";
49. answers[4] = "An overheated vacuum tube";
50. score += handleQuestion(1, "What was the first computer bug? ", correct,
51. answers);
52. document.writeln("<hr>");
53. correct = 2;
54. answers[1] = "";
55. answers[2] = "HyperText Markup Language";
56. answers[3] = "High Tech Manufacture Ltd.";
57. answers[4] = "";
58. score += handleQuestion(2, "What is HTML?", correct, answers);
59. document.writeln("<hr>");
60. document.writeln("<p>You scored " + score + " correct out of a possible 2");
61. //-->
62. </script>
63. Thank you for your time.
64. </body>
65. </html>
Can you say what aspect of the code is still most confusing?