JAVASCRIPT PROJECT 06

Build a JavaScript Quiz App

In this project, you will learn how to create an interactive Quiz App using JavaScript. The application will display questions, allow users to select answers, calculate scores and show the final result.

01

Project Introduction

A Quiz App presents a series of questions to the user. The user selects an answer and JavaScript checks whether the selected answer is correct.

What You Will Learn

  • JavaScript arrays
  • JavaScript objects
  • DOM manipulation
  • Click events
  • Conditional statements
  • Score calculation
  • Dynamic HTML
Question Display a question to the user.
Answer Check the selected option.
Score Calculate the final result.
02

Create the HTML Structure

First, create a container for the quiz.

<div class="quiz">

    <h1 id="question">
        Question
    </h1>

    <div id="options">
    </div>

    <button id="nextButton">
        Next
    </button>

</div>
JavaScript will dynamically change the question and answer options inside these elements.
03

Create Question Data

Store questions inside an array of objects.

const questions = [

    {
        question:
            "What does HTML stand for?",

        options: [
            "Hyper Text Markup Language",
            "High Text Machine Language",
            "Home Tool Markup Language",
            "Hyperlinks Text Mark Language"
        ],

        answer: 0
    }

];

The answer property stores the index of the correct option.

Arrays are useful when an application needs to store multiple questions.
04

Create Answer Options

JavaScript can create buttons dynamically for every answer option.

questions[currentQuestion]
    .options
    .forEach(function(option, index){

        const button =
            document.createElement("button");

        button.textContent =
            option;

        button.onclick =
            function(){

                checkAnswer(index);

            };

        optionsContainer
            .appendChild(button);

    });
createElement() creates a new HTML element using JavaScript.
05

Display the Question

Use the current question index to display the correct question.

questionElement.textContent =
    questions[currentQuestion].question;

Current Question Variable

let currentQuestion = 0;

The first question starts at index 0.

06

Check the Answer

When the user selects an option, compare its index with the correct answer.

function checkAnswer(selected){

    const correct =
        questions[currentQuestion].answer;

    if(selected === correct){

        console.log("Correct!");

    }
    else{

        console.log("Wrong!");

    }

}
The strict equality operator === checks whether two values are equal without automatic type conversion.
07

Create the Score System

Create a variable to store the user's score.

let score = 0;

Increase the score when the selected answer is correct.

if(selected === correct){

    score++;

}
Every correct answer increases the score by one.
08

Move to the Next Question

After answering a question, increase the current question index.

currentQuestion++;

Then display the next question.

currentQuestion++;

loadQuestion();
The loadQuestion() function can be responsible for updating the question and answer buttons.
09

Show Quiz Progress

Users should know which question they are currently answering.

const progress =
    currentQuestion + 1;

console.log(
    "Question " +
    progress +
    " of " +
    questions.length
);

For example:

Question 3 of 10
10

Display the Final Result

When all questions have been answered, show the user's final score.

const total =
    questions.length;

resultElement.textContent =
    "You scored " +
    score +
    " out of " +
    total;

Calculate Percentage

const percentage =
    (score / total) * 100;
Percentage makes the final result easier to understand.

Complete Project Flow

LOAD Question
SELECT Answer
CHECK Correct?
NEXT Question
RESULT Score
12

Complete Quiz Project

You have now learned the main concepts required to build an interactive JavaScript Quiz App.

Tutorial Completed! 🧠

Now open the complete working Quiz App and test your JavaScript knowledge.

Run Complete Project Learn JavaScript Arrays