Created
April 13, 2025 16:13
-
-
Save davidystephenson/8e6750361b0ceb80b80af9a0d21cdfb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let currentQuestion = 0 | |
let score = 0 | |
const numOfQuestions = questions.length | |
const numOfChoices = 3 | |
const questionCountElement = document.getElementById('question-count') | |
function displayCount () { | |
const countMessage = `${currentQuestion + 1}/${numOfQuestions}` | |
questionCountElement.innerText = countMessage | |
} | |
function displayQuestion () { | |
const question = questions[currentQuestion] | |
console.log('question', question) | |
const questionElement = document.getElementById('question') | |
questionElement.innerText = question.question | |
displayCount() | |
const choices = document.getElementsByClassName('choice') | |
console.log('choices', choices) | |
// choices[0].innerText = question.choices[0] | |
// choices[1].innerText = question.choices[1] | |
// choices[2].innerText = question.choices[2] | |
const choicesArray = [...choices] | |
choicesArray.forEach((choice, index) => { | |
choice.innerText = question.choices[index] | |
}) | |
} | |
function checkAnswer (questionIndex) { | |
const question = questions[currentQuestion] | |
const correct = questionIndex === question.correctAnswer | |
console.log('correct', correct) | |
if (correct) { | |
score += 1 // score++ | |
} | |
const more = currentQuestion < questions.length - 1 | |
if (more) { | |
currentQuestion += 1 | |
displayCount() | |
displayQuestion() | |
} else { | |
endQuiz() | |
} | |
} | |
function updateScore () { | |
const scoreElement = document.getElementById('score') | |
const last = currentQuestion === questions.length - 1 | |
if (last) { | |
const scoreMessage = `Final Score: ${score}/${questions.length}` | |
scoreElement.innerText = scoreMessage | |
} else { | |
const scoreMessage = `Current Score: ${score}` | |
scoreElement.innerText = scoreMessage | |
} | |
} | |
displayQuestion() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you