Quizzes are fun! They’re a great way of learning about new subjects, and they allow you to engage your audience with something fun and playful. From a developer’s perspective, “How do I make a JavaScript quiz?” is one of the most common questions asked by people learning web development, and for good reason.
Coding your own JavaScript quiz game is a fantastic learning exercise. It teaches you how to deal with events, manipulate the DOM, handle user input, and use local storage to track their score. When you have a basic quiz up and running, there are a whole bunch of possibilities to add more advanced functionality, such as pagination.
In this tutorial, I’ll walk you through how to make a quiz in JavaScript that you’ll be able to adapt to your needs and add to your own site. If you’d like to see what we’ll be ending up with, you can skip ahead and see the working JavaScript quiz code.
For more in-depth JavaScript knowledge and challenges, get out our free book: Learn to Code with JavaScript.
Here are a few things to know before starting:
Ideally, we want the quiz’s questions and answers to be in our JavaScript code and have our script automatically generate the quiz app. That way, we won’t need to write a lot of repetitive markup, and we can add and remove questions easily.
In this example, we’ll be structuring our project using three key files:
However, if you prefer, you can also include the CSS and JavaScript code directly within the HTML file as inline code.
When learning how to make a quiz in HTML and JavaScript, it’s important to understand how the HTML structure interacts with the JavaScript logic. So, as the first step, let’s set up the HTML structure of our JavaScript quiz game.
Here’s how that would look:
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
This structure is a simple example of how to create quiz HTML code that serves as a foundation for your JavaScript quiz template. If you run the application now, you will just see a “Submit Quiz” button.
Now, we can use the JavaScript document.getElementById method to select the above HTML elements and store references to them in the JavaScript quiz code like below:
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
The next thing our quiz app needs is some questions to display. We’ll use JavaScript object literals to represent the individual questions and an array to hold all of the questions that make up our quiz app. Using an array will make the questions easy to iterate over:
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
Feel free to put in as many questions or answers as you want.
Note: As this is an array, the questions will appear in the order they’re listed. If you want to sort the questions in any way before presenting them to the user, check out our quick tip on sorting an array of objects in JavaScript.
Now that we have our list of questions, we can show them on the page. For that, we will be using a function named buildQuix(). Let’s go through the following JavaScript line by line to see how it works:
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
First, we create an output variable to contain all the HTML output, including questions and answer choices.
Next, we can start building the HTML for each question. We’ll need to loop through each question like this:
myQuestions<span>.forEach( (currentQuestion<span>, questionNumber</span>) => { </span> <span>// the code we want to run for each question goes here </span><span>});</span>
For brevity, we’re using an arrow function to perform our operations on each question. Because this is in a forEach loop, we get the current value, the index (the position number of the current item in the array), and the array itself as parameters. We only need the current value and the index, which for our purposes, we’ll name currentQuestion and questionNumber respectively.
Now let’s look at the code inside our loop:
<span>// we'll want to store the list of answer choices </span><span>const answers = []; </span> <span>// and for each available answer... </span><span>for(letter in currentQuestion.answers){ </span> <span>// ...add an html radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span><span>} </span> <span>// add this question and its answers to the output </span>output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span><span>); </span>
For each question, we’ll want to generate the correct HTML. So, our first step is to create an array to hold the list of possible answers.s.
Next, we’ll use a loop to fill in the possible answers for the current question. For each choice, we’re creating an HTML radio button, which we enclose in a
Here, we’re using template literals, which are strings but more powerful. We’ll make use of the following features of template literals:
Once we have our list of answer buttons, we can push the question HTML and the answer HTML onto our overall list of outputs.
Notice that we’re using a template literal and some embedded expressions to first create the question div and then create the answer div. The join expression takes our list of answers and puts them together in one string that we can output into our answers div.
Now that we’ve generated the HTML for each question, we can join it all together and show it on the page:
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
Now, our buildQuiz function is complete, and you should be able to run the quiz app and see the questions displayed.
However, the structure of your code is important. Due to something called the temporal dead zone, you can’t reference your question array before it has been defined.
To recap, this is the correct structure:
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
At this point, we want to build out our showResults function to loop over the answers, check them, and show the results. This is a crucial part of any quiz game JavaScript implementation, as it provides immediate feedback to the user based on their performance.
Here’s the function, which we’ll go through in detail next:
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
First, we select all the answer containers in our quiz’s HTML. Then, we’ll create variables to keep track of the user’s current answer and the total number of correct answers.
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
Now, we can loop through each question and check the answers.
We will be using 3 steps for that:
Let’s look more closely at how we’re finding the selected answer in our HTML:
myQuestions<span>.forEach( (currentQuestion<span>, questionNumber</span>) => { </span> <span>// the code we want to run for each question goes here </span><span>});</span>
First, we’re making sure we’re looking inside the answer container for the current question.
In the next line, we’re defining a CSS selector that will let us find which radio button is checked.
Then we’re using JavaScript’s querySelector to search for our CSS selector in the previously defined answerContainer. In essence, this means that we’ll find which answer’s radio button is checked.
Finally, we can get the value of that answer by using .value.
What if the user has left an answer blank? In this case, using .value would cause an error because you can’t get the value of something that’s not there. To solve this, we’ve added ||, which means “or”, and {}, which is an empty object. Now, the overall statement says:
As a result, the value will either be the user’s answer or undefined, which means a user can skip a question without crashing our quiz app.
The next statements in our answer-checking loop will let us handle correct and incorrect answers.
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
If the user’s answer matches the correct choice, increase the number of correct answers by one and (optionally) color the set of choices green. If the answer is wrong or blank, color the answer choices red (again, optional).
Once the answer-checking loop is finished, we can show how many questions the user got right:
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
And now we have a working JavaScript quiz!
If you’d like, you can wrap the whole quiz in an IIFE (immediately invoked function expression), which is a function that runs as soon as you define it. This will keep your variables out of global scope and ensure that your quiz app doesn’t interfere with any other scripts running on the page.
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
Now you’re all set! Feel free to add or remove questions and answers and style the quiz however you like.
Now, if you run the application, you can select the answers and submit the quiz to get the results.
Since now we have a working quiz, let’s make it more user friendly by adding some styles. However, I won’t be going into details of each style. You can directly copy the below code into the styles.css file.
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
At this point, your quiz might look like this (with a tiny bit of styling):
As you can see in the above images, the questions in the quiz are ordered one after another. We have to scroll down to select our answers. Although this looks fine with three questions, you might start struggling to answer them when the number of questions increases. So, we need to find a way to show only one question at a time through pagination.
For that, you’ll need:
So, let’s make some adjustments to our code, starting with HTML:
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
Most of that markup is the same as before, but now we’ve added navigation buttons and a quiz container. The quiz container will help us position the questions as layers that we can show and hide.
Next, inside the buildQuiz function, we need to add a
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
Next, we can use some CSS positioning to make the slides sit as layers on top of one another. In this example, you’ll notice we’re using z-indexes and opacity transitions to allow our slides to fade in and out. Here’s what that CSS might look like:
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
Now we’ll add some JavaScript to make the pagination work. As before, the order is important, so this is the revised structure of our code:
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
We can start with some variables to store references to our navigation buttons and keep track of which slide we’re on. Add these after the call to buildQuiz(), as shown above:
myQuestions<span>.forEach( (currentQuestion<span>, questionNumber</span>) => { </span> <span>// the code we want to run for each question goes here </span><span>});</span>
Next we’ll write a function to show a slide. Add this beneath the existing functions (buildQuiz and showResults):
<span>// we'll want to store the list of answer choices </span><span>const answers = []; </span> <span>// and for each available answer... </span><span>for(letter in currentQuestion.answers){ </span> <span>// ...add an html radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span><span>} </span> <span>// add this question and its answers to the output </span>output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span><span>); </span>
Here’s what the first three lines do:
The next lines introduce the following JavaScript logic:
After we’ve written our function, we can immediately call showSlide(0) to show the first slide. This should come after the pagination code:
quizContainer<span>.innerHTML = output.join('');</span>
Next we can write functions to make the navigation buttons work. These go beneath the showSlide function:
<span>// Functions </span><span>function buildQuiz(){ ... } </span><span>function showResults(){ ... } </span> <span>// Variables </span><span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit'); </span><span>const myQuestions = [ ... ]; </span> <span>// Kick things off </span><span>buildQuiz(); </span> <span>// Event listeners </span>submitButton<span>.addEventListener('click', showResults); </span>
Here, we’re making use of our showSlide function to allow our navigation buttons to show the previous slide and the next slide.
Finally, we’ll need to hook the navigation buttons up to these functions. This comes at the end of the code:
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
Now your quiz has working navigation!
Now that you have a basic JavaScript quiz app, it’s time to get creative and experiment.
Here are some suggestions you can try:
Adding more questions to your JavaScript quiz is a simple process. You need to add more objects to the questions array in your JavaScript code. Each object represents a question and has two properties: text (the question itself) and responses (an array of possible answers). Here’s an example of how you can add a new question:
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
In this example, we’re adding a question about the capital of France, with four possible answers. The correct answer is marked with ‘correct: true’.
Randomizing the order of questions can make your quiz more challenging and fun. You can achieve this by using the sort() method combined with the Math.random() function. Here’s how you can do it:
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
This code will randomly sort the questions array each time the page is loaded.
Adding a timer can make your quiz more exciting. You can easily add a timer to the quiz using the JavaScript setInterval() function. Here’s a simple example:
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
In this example, the quiz will last for 30 seconds. The timer will update every second, and when the time is up, an alert will be shown.
You can show the correct answer by modifying the checkAnswer() function. You can add an else clause to the if statement that checks if the answer is correct. Here’s how you can do it:
<span><span><span><div</span> id<span>="quiz"</span>></span><span><span></div</span>></span> </span><span><span><span><button</span> id<span>="submit"</span>></span>Submit Quiz<span><span></button</span>></span> </span><span><span><span><div</span> id<span>="results"</span>></span><span><span></div</span>></span></span>
In this example, if the user’s answer is incorrect, an alert will be shown with the correct answer.
You can add images to your questions by adding an ‘image’ property to the question objects. You can then use this property to display the image in your HTML. Here’s an example:
<span>const quizContainer = document.getElementById('quiz'); </span><span>const resultsContainer = document.getElementById('results'); </span><span>const submitButton = document.getElementById('submit');</span>
In your HTML, you can display the image like this:
<span>const myQuestions = [ </span> <span>{ </span> <span>question: "Who invented JavaScript?", </span> <span>answers: { </span> <span>a: "Douglas Crockford", </span> <span>b: "Sheryl Sandberg", </span> <span>c: "Brendan Eich" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which one of these is a JavaScript package manager?", </span> <span>answers: { </span> <span>a: "Node.js", </span> <span>b: "TypeScript", </span> <span>c: "npm" </span> <span>}, </span> <span>correctAnswer: "c" </span> <span>}, </span> <span>{ </span> <span>question: "Which tool can you use to ensure code quality?", </span> <span>answers: { </span> <span>a: "Angular", </span> <span>b: "jQuery", </span> <span>c: "RequireJS", </span> <span>d: "ESLint" </span> <span>}, </span> <span>correctAnswer: "d" </span> <span>} </span><span>]; </span>
And in your JavaScript, you can update the src attribute of the image when displaying a new question:
<span>function buildQuiz(){ </span> <span>// variable to store the HTML output </span> <span>const output = []; </span> <span>// for each question... </span> myQuestions<span>.forEach( </span> <span>(currentQuestion<span>, questionNumber</span>) => { </span> <span>// variable to store the list of possible answers </span> <span>const answers = []; </span> <span>// and for each available answer... </span> <span>for(letter in currentQuestion.answers){ </span> <span>// ...add an HTML radio button </span> answers<span>.push( </span> <span><span>`<label> </span></span><span><span> <input type="radio" name="question<span>${questionNumber}</span>" value="<span>${letter}</span>"> </span></span><span><span> <span>${letter}</span> : </span></span><span><span> <span>${currentQuestion.answers[letter]}</span> </span></span><span><span> </label>`</span> </span> <span>); </span> <span>} </span> <span>// add this question and its answers to the output </span> output<span>.push( </span> <span><span>`<div > <span>${currentQuestion.question}</span> </div> </span></span><span><span> <div > <span>${answers.join('')}</span> </div>`</span> </span> <span>); </span> <span>} </span> <span>); </span> <span>// finally combine our output list into one string of HTML and put it on the page </span> quizContainer<span>.innerHTML = output.join(''); </span><span>}</span>
In this example, an image of an elephant will be displayed when the question is shown.
Handling multiple correct answers involves allowing the user to select more than one answer and checking if any of the selected answers are correct. For example, here is how you can update the above showResults() function to handle multiple correct answers.
myQuestions<span>.forEach( (currentQuestion<span>, questionNumber</span>) => { </span> <span>// the code we want to run for each question goes here </span><span>});</span>
Maintaining separate JavaScript and CSS files is not a must. However, it is generally considered a best practice since it improves the readability and maintainability of your code.
The above is the detailed content of How to Make a Simple JavaScript Quiz: Code Tutorial. For more information, please follow other related articles on the PHP Chinese website!