0% found this document useful (0 votes)
3 views5 pages

JavaScript Quiz App with Timer

The document contains a JavaScript implementation for a quiz application with multiple-choice questions. It includes functionalities for a timer, navigation between questions, saving answers, and submitting the exam to calculate the score. The quiz consists of five sample questions with options and correct answers defined in an array.

Uploaded by

David Oloruntola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

JavaScript Quiz App with Timer

The document contains a JavaScript implementation for a quiz application with multiple-choice questions. It includes functionalities for a timer, navigation between questions, saving answers, and submitting the exam to calculate the score. The quiz consists of five sample questions with options and correct answers defined in an array.

Uploaded by

David Oloruntola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

// Sample questions array - customize as needed

const questions = [

question: "What is the capital of France?",

options: ["Paris", "London", "Berlin", "Madrid"],

answer: "A"

},

question: "What is 2 + 2?",

options: ["3", "4", "5", "6"],

answer: "B"

},

question: "Which planet is known as the Red Planet?",

options: ["Earth", "Mars", "Jupiter", "Venus"],

answer: "B"

},

question: "What is the largest ocean on Earth?",

options: ["Atlantic", "Indian", "Arctic", "Pacific"],

answer: "D"

},

question: "Who wrote 'To Kill a Mockingbird'?",

options: ["Harper Lee", "J.K. Rowling", "Mark Twain", "Ernest Hemingway"],


answer: "A"

];

let currentQuestionIndex = 0;

let answers = new Array([Link]).fill(null);

let timeLeft = 30 * 60; // 30 minutes in seconds

let timerInterval;

function startTimer() {

timerInterval = setInterval(() => {

timeLeft--;

const minutes = [Link](timeLeft / 60);

const seconds = timeLeft % 60;

[Link]('timer').textContent = `Time Remaining: ${minutes}:$


{[Link]().padStart(2, '0')}`;

if (timeLeft <= 0) {

clearInterval(timerInterval);

submitExam();

}, 1000);

function loadQuestion(index) {

const question = questions[index];


[Link]('question-number').textContent = `Question ${index + 1} of $
{[Link]}`;

[Link]('question-text').textContent = [Link];

const optionsContainer = [Link]('options');

[Link] = '';

[Link]((option, i) => {

const label = [Link]('label');

const input = [Link]('input');

[Link] = 'radio';

[Link] = 'answer';

[Link] = [Link](65 + i); // A, B, C, D

if (answers[index] === [Link]) {

[Link] = true;

[Link](input);

[Link]([Link](` ${[Link](65 + i)}) ${option}`));

[Link](label);

});

updateNavigation();

function updateNavigation() {

[Link]('prev-btn').disabled = currentQuestionIndex === 0;

[Link]('next-btn').[Link] = currentQuestionIndex === [Link] - 1 ?


'none' : 'inline-block';

[Link]('submit-btn').[Link] = currentQuestionIndex === [Link] -


1 ? 'inline-block' : 'none';
}

function saveAnswer() {

const selected = [Link]('input[name="answer"]:checked');

if (selected) {

answers[currentQuestionIndex] = [Link];

function submitExam() {

clearInterval(timerInterval);

let score = 0;

[Link]((answer, index) => {

if (answer === questions[index].answer) {

score++;

});

[Link]('.exam-container').[Link] = 'none';

[Link]('results').[Link] = 'block';

[Link]('score').textContent = `You scored ${score} out of ${[Link]}.`;

[Link]('next-btn').addEventListener('click', () => {

saveAnswer();

if (currentQuestionIndex < [Link] - 1) {


currentQuestionIndex++;

loadQuestion(currentQuestionIndex);

});

[Link]('prev-btn').addEventListener('click', () => {

saveAnswer();

if (currentQuestionIndex > 0) {

currentQuestionIndex--;

loadQuestion(currentQuestionIndex);

});

[Link]('submit-btn').addEventListener('click', () => {

saveAnswer();

submitExam();

});

// Initialize

startTimer();

loadQuestion(currentQuestionIndex);

You might also like