Skip to content
Tutorials Center
My learning

Topics

Lesson 78 of 86 ยท Projects

Project: Timed Quiz Trainer

Build a live timed aptitude quiz in HTML and JavaScript with a countdown per question, negative marking, skipping, a score summary and a review.

Aptitude tests are as much about time and discipline as about maths. You can know every formula and still lose marks by lingering on one question or guessing blindly when wrong answers are penalized. The best way to build both habits is to practice under the same rules as the real test: a clock that keeps running, a penalty for wrong answers and the option to skip.

In this project you will build a small timed quiz trainer in plain HTML, CSS and JavaScript. It asks eight questions, runs a countdown for each, awards +1 for a right answer, takes off 0.25 for a wrong one and gives 0 for a skip or a timeout. At the end it shows your score, accuracy and a review. You can edit the question list and use it as your own practice tool.

The design

The app is a tiny state machine with three screens and one repeating loop.

flowchart TB
    A["Intro screen: choose time limit"] -->|"Start"| B["Show question, start countdown"]
    B -->|"Option clicked"| C["Mark right or wrong, stop timer"]
    B -->|"Skip pressed"| D["Mark skipped, stop timer"]
    B -->|"Timer reaches 0"| E["Mark timeout, stop timer"]
    C --> F["Show explanation and Next"]
    D --> F
    E --> F
    F -->|"More questions"| B
    F -->|"Last question"| G["Results and review"]

Every question ends in exactly one of four outcomes: right, wrong, skipped or timeout.

Four design decisions are worth knowing before you read the code:

  • Data first. The questions live in one array of objects with q (text), options, answer (the index of the right option, counted from 0) and why (the explanation). Adding a question means adding one object.
  • Marks in quarters. The code counts in quarter marks as whole numbers: +4 for right, -1 for wrong. It divides by 4 only for display. Whole-number arithmetic is always exact, so any penalty that is a multiple of a quarter fits, and it is a common trick for avoiding rounding errors with money, marks and measurements (a penalty such as 0.1 or 1/3 cannot be stored exactly as a decimal fraction in binary).
  • One timer per question. setInterval ticks every second, finish clears it. A locked flag stops a double answer, for example a click arriving as the timer expires.
  • One function ends a question. Click, skip and timeout all call finish(choice, timedOut), so scoring lives in a single place.

Should you guess?

Negative marking turns guessing into a calculation. If you pick at random among k remaining options, the chance of being right is 1/k. With a penalty p for a wrong answer, the expected gain is 1/k - (1 - 1/k) x p. This script tabulates it.

JS Expected value of a guess

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The numbers tell a clear story. With a 0.25 penalty and four options, a blind guess is worth a tiny positive amount (+0.0625). If you can eliminate even one option, a guess becomes clearly worthwhile. With a one-third penalty, a blind guess among four is worth exactly nothing, and with a half-mark penalty it loses on average. The break-even penalty for k options is 1 / (k - 1), so know the exam's rule and decide before the test whether to guess when stuck.

The trainer

Run it, answer a few questions, let one time out and skip one. Try the shorter time limits to feel the pressure.

Timed quiz trainer

Reading the code

  • show(name) switches screens by toggling the hidden attribute, so there is no page reload. The line [hidden] { display: none !important; } guarantees it works even when other CSS sets display.
  • showQuestion builds the option buttons with document.createElement and textContent, never with an HTML string built from question text, which keeps the page safe if you later load questions from elsewhere.
  • finish is the single place where outcomes are decided and scores updated. It also colors the right answer green and your wrong choice red, which is how good trainers teach: show the correct answer immediately, with the reason.
  • showResults counts outcomes with filter, computes accuracy only on attempted questions (right + wrong) and builds a review table.

Ideas to extend it

  1. Shuffle the questions (and options) with a Fisher-Yates shuffle each run, remembering to shuffle the answer index along with the options.
  2. Section timer: replace the per-question timer with one total timer for the whole set, as in many real tests.
  3. Per-topic tags: add a topic field to each question and show accuracy by topic in the review, so you see which chapter to revisit.
  4. Skip penalty modes: let the user pick between "-0.25", "-1/3" or no penalty, and show the break-even guess calculation from above.
  5. Generate questions with the next project, Random Question Generator, so the quiz never runs out.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the trainer store marks in quarters (+4 and -1) instead of +1 and -0.25?

    Adding whole numbers is exact. Decimal fractions such as 0.1 and 0.2 are not exactly representable in binary floating point, so working in a whole-number unit and converting for display is safer.
  2. 2. With a penalty of 0.25 for a wrong answer, what is the expected gain of a blind guess among 4 options?

    Expected gain = 1/4 x 1 - 3/4 x 0.25 = 0.25 - 0.1875 = 0.0625, a small positive value.
  3. 3. A student answers 6 right, 1 wrong, and skips 1 of 8 questions with the +1 / -0.25 rule. What is the score?

    6 x 1 = 6, minus 0.25 for the wrong answer gives 5.75, and the skip adds nothing, so the score is 5.75.
  4. 4. What is the benefit of calling one function, finish, for a click, a skip and a timeout?

    One function for all three endings keeps the logic consistent. The locked flag is still needed to stop a second action arriving after the first.

Summary

  • A timed trainer is a small state machine: intro, question loop with a countdown, feedback, results.
  • Keep the question data in one array and the scoring in one function.
  • Count marks in whole quarters to avoid rounding errors, and convert only for display.
  • Expected value explains when guessing pays: gain = 1/k - (1 - 1/k) x penalty, with break-even penalty 1/(k - 1).
  • Practicing under timed, penalized conditions builds the pace and discipline a real test demands.

Next: Project: Random Question Generator, where you will generate endless practice questions with answers checked by code.