JavaScript project
Quiz App
Build a multiple-choice quiz that checks answers, tracks your score, and lets you start again.
- Objects
- Arrays
- Application state
How to run this project
- Create a file named
index.htmlin a new folder. - Copy the complete code below into the file and save it.
- Open
index.htmlin your browser. No installation or API key is needed.
Complete project code
This single file includes the HTML interface, basic CSS styling, and JavaScript behavior. The script runs after the HTML so all the elements are available.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quiz App</title>
<style>
body { font-family: system-ui, sans-serif; max-width: 640px; margin: 40px auto; padding: 0 20px; line-height: 1.6; }
form { display: grid; gap: 12px; }
input, select, button { font: inherit; padding: 8px; }
button { cursor: pointer; }
button:disabled { cursor: default; }
li { margin: 12px 0; overflow-wrap: anywhere; }
li button { margin-left: 12px; }
input[type="checkbox"] { margin-right: 8px; }
#answers { display: grid; gap: 8px; }
</style>
</head>
<body>
<h1>JavaScript Quiz</h1>
<p id="progress" role="status"></p>
<h2 id="question" tabindex="-1"></h2>
<div id="answers" aria-labelledby="question"></div>
<p id="feedback" role="status"></p>
<button id="next" type="button" hidden>Next question</button>
<button id="restart" type="button" hidden>Try again</button>
<script>
const questions = [
{
text: "Which keyword declares a variable that can be reassigned?",
options: ["const", "let", "return"],
answer: 1
},
{
text: "Which method adds an item to the end of an array?",
options: ["push()", "filter()", "map()"],
answer: 0
},
{
text: "Which event fires when a button is clicked?",
options: ["submit", "change", "click"],
answer: 2
}
];
const progress = document.querySelector("#progress");
const heading = document.querySelector("#question");
const answers = document.querySelector("#answers");
const feedback = document.querySelector("#feedback");
const next = document.querySelector("#next");
const restart = document.querySelector("#restart");
let currentIndex = 0;
let score = 0;
let answered = false;
function showQuestion() {
const question = questions[currentIndex];
answered = false;
progress.textContent = "Question " + (currentIndex + 1) + " of " + questions.length;
heading.textContent = question.text;
answers.replaceChildren();
feedback.textContent = "";
next.hidden = true;
restart.hidden = true;
question.options.forEach((option, index) => {
const button = document.createElement("button");
button.type = "button";
button.textContent = option;
button.addEventListener("click", () => checkAnswer(index));
answers.append(button);
});
}
function checkAnswer(selectedIndex) {
if (answered) return;
answered = true;
const question = questions[currentIndex];
if (selectedIndex === question.answer) {
score++;
feedback.textContent = "Correct!";
} else {
feedback.textContent = "Correct answer: " + question.options[question.answer];
}
answers.querySelectorAll("button").forEach((button) => {
button.disabled = true;
});
next.textContent = currentIndex === questions.length - 1 ? "See score" : "Next question";
next.hidden = false;
next.focus();
}
next.addEventListener("click", () => {
if (!answered) return;
currentIndex++;
if (currentIndex < questions.length) {
showQuestion();
heading.focus();
} else {
heading.textContent = "Quiz complete!";
progress.textContent = "Score: " + score + " / " + questions.length;
answers.replaceChildren();
feedback.textContent = "";
next.hidden = true;
restart.hidden = false;
restart.focus();
}
});
restart.addEventListener("click", () => {
currentIndex = 0;
score = 0;
showQuestion();
heading.focus();
});
showQuestion();
</script>
</body>
</html>How the code works
1. Represent questions as data
Each object in questions contains question text, an options array, and the index of the correct answer. Array indexes start at 0, so answer: 1 means the second option is correct. You can add questions using the same structure.
2. Track quiz state
currentIndex identifies the question being displayed, score counts correct answers, and answered records whether the current question has already been attempted. Keeping this state in variables allows the interface to change as the learner progresses.
3. Create the answer buttons
showQuestion reads the current question, resets feedback, and clears previous buttons. forEach creates one button per option. Each click listener remembers its option index and passes that index to checkAnswer.
4. Check each answer once
checkAnswer compares selectedIndex with the stored correct index using strict equality. A correct answer increments score. The answered guard and disabled buttons prevent repeated clicks from increasing the score. Feedback reveals the correct answer before the learner continues.
5. Advance, show the result, and restart
The Next listener increments currentIndex. If another question exists, it calls showQuestion; otherwise it displays the final score and reveals Try again. Restart resets both the index and score. Explicit focus changes keep keyboard navigation usable when controls are replaced or hidden.
Try it yourself
Choose let, push(), and click to earn 3 / 3. Restart and select an incorrect answer to check the feedback. Try clicking an answer repeatedly: it should only count once.
