Educational JavaScripting : Exercises

[Workshop] [Contents] [Experiments] [First Page] [Custom] [Quiz/Score] [Story] [Quiz/Timed] [Quiz/Math] [Stats]


A Timed Quiz

Summary: We use the setTimeout() function and the location object to build a timed quiz.

Prerequisites: Experience building forms and decoding query strings.

Provides: Experience with timers. Ideas for interactive quizzes.

References: Time and timers in JavaScript

Overview:


Introduction

When writing quizzes for our students, we often like to limit the amount of time they spend on one problem (or, preferably, to give them feedback before they spend too much time going in the wrong direction). In this exercise, we'll build a simple quiz that exits after a specified amount of time.


Build a Quiz

Make a copy of your quiz from a previous exercise. Update it if you think that is necessary.


Create a Results Page

Build a page on which you will display results. You may want this page to take all the fields from your quiz as part of the query, or you may want to simply take a score as the query. In the latter case, your document might include something like the following.

<script language="javascript">
var numgrade // The student's score on the quiz
numgrade = queryField("score")
if (numgrade>=90) {
  document.writeln("<p>You got an A on the quiz</p>")
}
else if (numgrade>=80) {
  document.writeln("<p>You got a B on the quiz</p>")
}
else if (numgrade>70) {
  document.writeln("<p>You got a C on the quiz</p>")
}
else {
  document.writeln("<p>I expect better of you</p>")
}
</script>


Add a Timer

Link your two pages together with a timer. In essence, you will add a setTimeout() routine to the beginning or end of your page (the end may be better, so that the timer starts after the student has started to read). Here's a somewhat complicated version.

<script language="javascript">
function timesUp()
{
  score = calculateScore()
  location = "results.html?score=" + score
}
setTimeout("timesUp()",60000)
</script>


[Workshop] [Contents] [Experiments] [First Page] [Custom] [Quiz/Score] [Story] [Quiz/Timed] [Quiz/Math] [Stats]

This page created Wed Jun 11 14:41:21 1997 by SamR's Site Suite.