Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a quiz in JavaScript

Getting Started

Creating a quiz is a fantastic way to test your JavaScript skills. In this blog post, we will guide you step by step on how to make a quiz in JavaScript. Let's dive in.

Understanding the Structure of a Quiz

Before we start coding, let's break down what a basic quiz usually consists of. It's a bit like a football game. In a football game, there are teams (questions), players (answers), and a score. Our JavaScript quiz will be similar - we'll have questions, options for answers, and a scoring system.

Step 1: Creating Questions and Answers

Firstly, we need to create our questions and answers. In JavaScript, we can store our questions and answers in an object. An object in JavaScript is like a box where we can store different types of data (like strings of text, numbers, and even other objects).

Here is an example of how we can create a question:

var question1 = {
    question: "What is the capital of Australia?",
    options: ["Sydney", "Melbourne", "Canberra", "Perth"],
    answer: "Canberra"
};

In this code snippet, question1 is an object that stores the first question of our quiz. It has three properties: question, options, and answer. The question property is a string that contains the question. The options property is an array (think of it like a list) containing the possible answers. The answer property is a string that contains the correct answer.

Step 2: Displaying the Questions and Options

Now that we have our questions and answers, we need to display them to the user. We can do this by manipulating the HTML of our webpage using JavaScript. This is like using JavaScript as a remote control to change the content of our webpage.

Here's how we can display the first question and its options:

document.getElementById("question").innerText = question1.question;
document.getElementById("option1").innerText = question1.options[0];
document.getElementById("option2").innerText = question1.options[1];
document.getElementById("option3").innerText = question1.options[2];
document.getElementById("option4").innerText = question1.options[3];

In this code, document.getElementById("question") is like telling JavaScript: "Find the element on the webpage with the id 'question'." The .innerText is like saying: "Change the text inside that element to...".

Step 3: Checking the Answer

We need a way to check if the user's answer is correct. We can do this by creating a function. A function in JavaScript is like a machine - it takes in some input, does something with it, and then gives back some output.

Here's an example of a function that checks the answer:

function checkAnswer(userAnswer) {
    if (userAnswer === question1.answer) {
        alert("Correct!");
    } else {
        alert("Sorry, that's not correct.");
    }
}

In this code, checkAnswer is a function that takes in userAnswer (the answer chosen by the user) as input. The if statement is like a fork in the road - if the user's answer is the same as the correct answer (userAnswer === question1.answer), it will display the message "Correct!", otherwise, it will display "Sorry, that's not correct.".

Step 4: Keeping Score

Now, let's add a scoring system. We can create a variable to keep track of the user's score. In JavaScript, a variable is like a box where we can store a single piece of data.

Here's how we can create a score variable and update it when the user answers correctly:

var score = 0;

function checkAnswer(userAnswer) {
    if (userAnswer === question1.answer) {
        alert("Correct!");
        score = score + 1;
    } else {
        alert("Sorry, that's not correct.");
    }
}

In this code, score = score + 1; is like saying: "Add one to the score."

Step 5: Moving to the Next Question

Finally, we need to move to the next question after the user answers. We can do this by creating a new function that updates the question and options:

function nextQuestion(question) {
    document.getElementById("question").innerText = question.question;
    document.getElementById("option1").innerText = question.options[0];
    document.getElementById("option2").innerText = question.options[1];
    document.getElementById("option3").innerText = question.options[2];
    document.getElementById("option4").innerText = question.options[3];
}

Then we can use this function to move to the next question when the user answers:

var currentQuestion = 0;

function checkAnswer(userAnswer) {
    if (userAnswer === questions[currentQuestion].answer) {
        alert("Correct!");
        score = score + 1;
    } else {
        alert("Sorry, that's not correct.");
    }

    currentQuestion = currentQuestion + 1;
    nextQuestion(questions[currentQuestion]);
}

Notice that we're now using an array of questions (questions) and a variable (currentQuestion) to keep track of which question we're on.

Conclusion

Great job, you have now created a simple quiz using JavaScript! Remember, creating a quiz is like setting up a football game. You've created the teams (questions), added the players (the answer options), set the scores, and even moved from one match to another (next question).

But just like in a real football game, there's always room for improvements. You can add more questions, include images or even sound. The choice is yours. So, unleash your creativity and make your quiz as fun and challenging as you want!

Remember, programming is not about knowing everything. It's about being excited to learn new things. So keep practicing, keep building, and most importantly, keep having fun!