Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Quiz App in JavaScript

Score a two-question quiz in JavaScript using an array and radio-button forms. Convert choice indexes to numbers; answers are visible in the page code.

IntermediateJavaScript Mini ProjectsExample 5 of 20

Save this as index.html and open it in a browser.

quiz-app.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<form id="quiz"><h1 id="question"></h1><div id="choices"></div><button>Submit answer</button></form><p id="result"></p>
7<script>
8const questions = [
9 { text: 'Which value is a number?', options: ['"7"', '7'], correct: 1 },
10 { text: 'Which method adds to an array?', options: ['push()', 'trim()'], correct: 0 }
11];
12let current = 0, score = 0;
13const form = document.querySelector('#quiz');
14function render() {
15 document.querySelector('#question').textContent = questions[current].text;
16 const choices = document.querySelector('#choices'); choices.replaceChildren();
17 questions[current].options.forEach((text, index) => {
18 const label = document.createElement('label');
19 const input = document.createElement('input');
20 input.type = 'radio'; input.name = 'answer'; input.value = index; input.required = true;
21 label.append(input, document.createTextNode(text)); choices.append(label);
22 });
23}
24form.onsubmit = event => {
25 event.preventDefault();
26 const answer = form.querySelector('input:checked');
27 if (!answer) return;
28 if (Number(answer.value) === questions[current].correct) score++;
29 current++;
30 if (current === questions.length) {
31 form.hidden = true;
32 document.querySelector('#result').textContent = 'Score: ' + score + '/' + questions.length;
33 } else render();
34};
35render();
36</script>
37</body>
38</html>

Sample Output

Score: 2/2

The selected option indexes matched the stored correct indexes on both questions. The completed form is hidden.

In this one

  • Store questions and correct indexes
  • Render one radio group at a time
  • Convert answer strings to numbers
  • Stop after the final question

How it works

  1. 1

    Create two question objects with zero-based correct indexes.

  2. 2

    Render the current text and radio choices.

  3. 3

    Require a selection and handle the form locally.

  4. 4

    Compare the selected numeric index, update score, and advance current.

  5. 5

    After two correct answers, hide the form and display Score: 2/2.

Edge cases

No choice selected

An empty answer should not count as a submission.

The radio inputs are required, and the handler also checks for a selected input.

Quiz already complete

Reading questions[current] after the final question would fail.

The completion branch hides the form instead of calling render past the last array index.

What's going on

A quiz app shows a question, accepts one answer, and keeps a score. This two-question example is small enough to trace by hand. It helps a student practise arrays, form input, and a simple sequence of screens.

Each question stores its text, choices, and the index of the correct choice. An index is a position counted from zero. The first option has index 0, and the second has index 1. render creates a group of radio buttons so only one answer can be selected for the current question.

Submitting converts the selected value to a number before comparing it with correct. The app updates the score at most once per submission, advances current, and either renders the next question or shows the final score. The form disappears after completion, so clicking Submit cannot access a question beyond the array. Answers live in the page code; this is a learning quiz, not a secure examination system.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the index of the first choice?

Pick an answer to check it.

Question 2

When does current increase?

Pick an answer to check it.

Questions people ask

Why is correct 1 for the first question?

The second option is the numeric value 7. Array positions start at zero.

Why convert answer.value?

Form input values are strings. Number makes the selected index comparable with the stored numeric index.

Can students inspect the answers?

Yes. The questions and answers are in browser code, so do not use this as a protected exam.