0% found this document useful (0 votes)
33 views

Quiz Exercises

The document describes a JavaScript quiz with multiple choice questions and discusses exercises to help understand the quiz code. The exercises ask the reader to trace variable values, identify code structures, suggest changes to the scoring logic and organization, and describe how the code works. Additional questions are provided to generate discussion about understanding the quiz code.

Uploaded by

Ali El Heddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Quiz Exercises

The document describes a JavaScript quiz with multiple choice questions and discusses exercises to help understand the quiz code. The exercises ask the reader to trace variable values, identify code structures, suggest changes to the scoring logic and organization, and describe how the code works. Additional questions are provided to generate discussion about understanding the quiz code.

Uploaded by

Ali El Heddad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercises on the javascript quiz

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?

c. What value does answers[3] have?

d. If userAnswerNumber has value 2, what value does answers[userAnswerNumber] have?

e. The html code produced by the function will be viewed as:


(i) (ii)

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>

6. <h1 align=center>CPSC101/WMST201 JavaScript Lab Sample Quiz</h1>


7. <form name=handoutquiz method=get action=handoutquiz_result.htm>
8. <table height="438">

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;

9. if ((i = lString.indexOf(number-1 + "=")) >= 0) {


10. i = lString.indexOf("=", i);
11. j = lString.indexOf("&", i);
12. }
13. if (i < 0 || j < 0) {
14. userAnswerNumber = 0;
15. }
16. else {
17. userAnswerNumber = parseInt(lString.substring(i + 1, j))+1;
18. }
19. document.writeln("<p><strong>Question " + number + ":</strong>");
20. document.writeln("<blockquote>");
21. document.writeln(text);
22. document.writeln("</blockquote>");
23. document.writeln("The correct answer is");
24. document.writeln("<blockquote>");
25. document.writeln(answers[correctAnswerNumber]);
26. document.writeln("</blockquote>");

27. if (userAnswerNumber == correctAnswerNumber) {


28. questionScore = 1;
29. }
30. document.writeln("You answered:");
31. document.writeln("<blockquote>");
32. document.writeln(answers[userAnswerNumber]);
33. document.writeln("</blockquote>");
34. document.writeln("</p>");
35. return questionScore;
36. } // end handleQuestion()

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>

(tear here and hand in at the end of class)

Rate your level of understanding of the quiz code.

I really don’t follow it at all


I have some idea of what it is doing
I understand it well enough to make some small changes
I have a very good understanding of the code

Can you say what aspect of the code is still most confusing?

What might help you understand the code better?

You might also like