SlideShare a Scribd company logo
1 of 5
Download to read offline
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

More Related Content

More from Augstore

Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdfNot yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
Augstore
 
Objectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdfObjectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdf
Augstore
 

More from Augstore (20)

Note the image showing a series of batholiths on the landscape- How di.pdf
Note the image showing a series of batholiths on the landscape- How di.pdfNote the image showing a series of batholiths on the landscape- How di.pdf
Note the image showing a series of batholiths on the landscape- How di.pdf
 
note 2^R stand for w reversed (c) L3-{ww-wR-w is odd with 010 as the m.pdf
note 2^R stand for w reversed (c) L3-{ww-wR-w is odd with 010 as the m.pdfnote 2^R stand for w reversed (c) L3-{ww-wR-w is odd with 010 as the m.pdf
note 2^R stand for w reversed (c) L3-{ww-wR-w is odd with 010 as the m.pdf
 
Not yet answered Points out of 1-00 Flag question It seems kind of wei.pdf
Not yet answered Points out of 1-00 Flag question It seems kind of wei.pdfNot yet answered Points out of 1-00 Flag question It seems kind of wei.pdf
Not yet answered Points out of 1-00 Flag question It seems kind of wei.pdf
 
Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdfNot yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
Not yet answered Points out of 1-00 Flag question Stephanie likes to p.pdf
 
Not for Profit class The non-profit is planned parenthood Discuss ho.pdf
Not for Profit class The non-profit is planned parenthood   Discuss ho.pdfNot for Profit class The non-profit is planned parenthood   Discuss ho.pdf
Not for Profit class The non-profit is planned parenthood Discuss ho.pdf
 
Not long ago- the Federal Communications Commission (FCC) implemented.pdf
Not long ago- the Federal Communications Commission (FCC) implemented.pdfNot long ago- the Federal Communications Commission (FCC) implemented.pdf
Not long ago- the Federal Communications Commission (FCC) implemented.pdf
 
Nonchalant Actron (NA) has a current dividend (D0) of $1-80- Dividends.pdf
Nonchalant Actron (NA) has a current dividend (D0) of $1-80- Dividends.pdfNonchalant Actron (NA) has a current dividend (D0) of $1-80- Dividends.pdf
Nonchalant Actron (NA) has a current dividend (D0) of $1-80- Dividends.pdf
 
Non-foliated metamorphic rocks generally are- composed of a few visibl.pdf
Non-foliated metamorphic rocks generally are- composed of a few visibl.pdfNon-foliated metamorphic rocks generally are- composed of a few visibl.pdf
Non-foliated metamorphic rocks generally are- composed of a few visibl.pdf
 
Non-financial reporting With the growth in non-financial reporting ove.pdf
Non-financial reporting With the growth in non-financial reporting ove.pdfNon-financial reporting With the growth in non-financial reporting ove.pdf
Non-financial reporting With the growth in non-financial reporting ove.pdf
 
Nominal GDP of Utopia was the highest in Real GDP of Utopia in 2016 is.pdf
Nominal GDP of Utopia was the highest in Real GDP of Utopia in 2016 is.pdfNominal GDP of Utopia was the highest in Real GDP of Utopia in 2016 is.pdf
Nominal GDP of Utopia was the highest in Real GDP of Utopia in 2016 is.pdf
 
Non-marine (live in water that is not seawater) diatoms are algae that.pdf
Non-marine (live in water that is not seawater) diatoms are algae that.pdfNon-marine (live in water that is not seawater) diatoms are algae that.pdf
Non-marine (live in water that is not seawater) diatoms are algae that.pdf
 
No outstanding checks and no deposits in transit were noted in August- (1).pdf
No outstanding checks and no deposits in transit were noted in August- (1).pdfNo outstanding checks and no deposits in transit were noted in August- (1).pdf
No outstanding checks and no deposits in transit were noted in August- (1).pdf
 
Nittany Company uses a periodic inventory system- At the end of the an.pdf
Nittany Company uses a periodic inventory system- At the end of the an.pdfNittany Company uses a periodic inventory system- At the end of the an.pdf
Nittany Company uses a periodic inventory system- At the end of the an.pdf
 
NIKE- INC- Condensed Balance Sheet May 31 (-$ in millions) Assets Curr.pdf
NIKE- INC- Condensed Balance Sheet May 31 (-$ in millions) Assets Curr.pdfNIKE- INC- Condensed Balance Sheet May 31 (-$ in millions) Assets Curr.pdf
NIKE- INC- Condensed Balance Sheet May 31 (-$ in millions) Assets Curr.pdf
 
News Revlew 4- Continuous Process Improvement Program Upon successful.pdf
News Revlew 4- Continuous Process Improvement Program Upon successful.pdfNews Revlew 4- Continuous Process Improvement Program Upon successful.pdf
News Revlew 4- Continuous Process Improvement Program Upon successful.pdf
 
Nick is a male who displays a rare mitochondrial disease- Nick's wife-.pdf
Nick is a male who displays a rare mitochondrial disease- Nick's wife-.pdfNick is a male who displays a rare mitochondrial disease- Nick's wife-.pdf
Nick is a male who displays a rare mitochondrial disease- Nick's wife-.pdf
 
Ninition min sinsMiate Orese sin tins 1230.pdf
Ninition min sinsMiate Orese sin tins 1230.pdfNinition min sinsMiate Orese sin tins 1230.pdf
Ninition min sinsMiate Orese sin tins 1230.pdf
 
nog(n)+n2-Theta(n2+sqrt(n)) True False.pdf
nog(n)+n2-Theta(n2+sqrt(n)) True False.pdfnog(n)+n2-Theta(n2+sqrt(n)) True False.pdf
nog(n)+n2-Theta(n2+sqrt(n)) True False.pdf
 
Observations- After 10 minutes the student makes the following observa.pdf
Observations- After 10 minutes the student makes the following observa.pdfObservations- After 10 minutes the student makes the following observa.pdf
Observations- After 10 minutes the student makes the following observa.pdf
 
Objectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdfObjectives- In this lab- students will practice- 1- Stack with single.pdf
Objectives- In this lab- students will practice- 1- Stack with single.pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 

Recently uploaded (20)

FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 

Not able to run quiz question -- Define an array of objects with qu.pdf

  • 1. 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?",
  • 2. 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() {
  • 3. 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"; }
  • 4. } // 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;
  • 5. hintButton.disabled = true; resultImage.src = ""; scoreDisplay.textContent = `You scored ${userScore} out of ${questions.length} (${Math.round(userScore/questions