Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Not able to run quiz question // Define an array of objects with questions, choices, and correct answers const questions = [ { question: What

Not able to run quiz question

// Define an array of objects with questions, choices, and correct answers

const questions = [

{

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

choices: ["Paris", "Berlin", "Madrid"],

answer: "Paris",

hint: "It's known as the 'City of Love'",

},

{

question: "What is the highest mountain in the world?",

choices: ["Mount Everest", "Mount Kilimanjaro", "Mount Fuji"],

answer: "Mount Everest",

hint: "It's located in the Himalayas",

},

{

question: "What is the largest planet in our solar system?",

choices: ["Jupiter", "Mars", "Venus"],

answer: "Jupiter",

hint: "It has the most moons of any planet",

},

{

question: "What is the smallest country in the world?",

choices: ["Vatican City", "Monaco", "San Marino"],

answer: "Vatican City",

hint: "It's located within Rome",

},

{

question: "What is the highest waterfall in the world?",

choices: ["Angel Falls", "Niagara Falls", "Victoria Falls"],

answer: "Angel Falls",

hint: "It's located in Venezuela",

}

];

// Get references to HTML elements

const questionText = document.getElementById("question-text");

const choiceList = document.getElementById("choice-list");

const submitButton = document.getElementById("submit-button");

const hintButton = document.getElementById("hint-button");

const resultImage = document.getElementById("result-image");

const scoreDisplay = document.getElementById("score-display");

// Define variables to track quiz progress and score

let currentQuestion = 0;

let userScore = 0;

// Display the current question and choices

function displayQuestion() {

questionText.textContent = questions[currentQuestion].question;

choiceList.innerHTML = "";

for (let i = 0; i < questions[currentQuestion].choices.length; i++) {

const choice = questions[currentQuestion].choices[i];

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

const radio = document.createElement("input");

radio.type = "radio";

radio.name = "choice";

radio.value = choice;

li.appendChild(radio);

li.appendChild(document.createTextNode(choice));

choiceList.appendChild(li);

}

}

// Check if the user's answer is correct and update the score

function checkAnswer() {

const selectedChoice = document.querySelector('input[name="choice"]:checked').value;

if (selectedChoice === questions[currentQuestion].answer) {

userScore++;

resultImage.src = "img/correct.png";

} else {

resultImage.src = "img/incorrect.png";

}

}

// Display a hint for the current question

function displayHint() {

const hint = questions[currentQuestion].hint;

if (hint) {

hintButton.textContent = hint;

}

}

// Move to the next question or end the quiz if all questions have been answered

function nextQuestion() {

if (currentQuestion < questions.length - 1) {

currentQuestion++;

displayQuestion();

hintButton.textContent = "Show Hint";

} else {

endQuiz();

}

}

// End the quiz and display the user's score

function endQuiz() {

questionText.textContent = "Quiz complete!";

choiceList.innerHTML = "";

submitButton.disabled = true;

hintButton.disabled = true;

resultImage.src = "";

scoreDisplay.textContent = `You scored ${userScore} out of ${questions.length} (${Math.round(userScore/questions

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions