I’m trying to create a second game mode in a js quiz. I have one game mode running as expected and another set of questions in another array waiting to be used. However, I’m very new to js and struggling how to pass the second array through all my code without altering the first set of data.
I’ve tried an if statement that uses the 2nd array but then the rest of the functions stop working. I’ve tried passing the array through my createQuestion function but it creates errors in the console. Such as
Uncaught TypeError: Cannot read properties of undefined (reading '1')
at createQuestion (script.js:184:35)
at HTMLButtonElement.nextQuestion (script.js:236:5)
The 2nd array I would like to pass through looks like this
let quizData2 = [
{
question: "assets/images/year/Brazil98-00.avif",
answers: ["1997", "1999", "2002", "1995"],
correct: "1999",
},
{
question: "assets/images/year/Denmark-92.webp",
answers: ["1991", "1990", "1995", "1992"],
correct: "1992",
},
When the start guess shirt year function is called I would like the 2nd arrays data set to be used.
document.getElementById('guess-shirt-start-btn').addEventListener('click', startGuessShirtTeam);
document.getElementById('guess-year-start-btn').addEventListener('click', startGuessShirtYear);
// function to start the Guess the Shirt type game
function startGuessShirtTeam () {
document.getElementById('game-choices-container').classList.add('hidden');
document.getElementById('question-container').classList.remove('hidden');
gameMode = 'shirt-quiz'
modeOfGame();
console.log(gameMode)
createQuestion(quizData1);
displayNumberOfQuestions();
};
function startGuessShirtYear () {
document.getElementById('game-choices-container').classList.add('hidden');
document.getElementById('question-container').classList.remove('hidden');
gameMode = 'shirt-year'
modeOfGame();
console.log(gameMode)
createQuestion(quizData2)
displayNumberOfQuestions();
}
I can get the first question from both arrays displaying but my subsequent check answer and next question functions stop working.
function createQuestion (questionArray) {
displayQuestionTitle();
shirtImage.src = questionArray[questionNumber].question;
questionArray[questionNumber].answers.forEach((o) => {
document.getElementById('answer-button-1').innerText = questionArray[questionNumber].answers[0]
document.getElementById('answer-button-2').innerText = questionArray[questionNumber].answers[1]
document.getElementById('answer-button-3').innerText = questionArray[questionNumber].answers[2]
document.getElementById('answer-button-4').innerText = questionArray[questionNumber].answers[3]
});
document.getElementById('answer-button-1').addEventListener('click', checkAnswer);
document.getElementById('answer-button-2').addEventListener('click', checkAnswer);
document.getElementById('answer-button-3').addEventListener('click', checkAnswer);
document.getElementById('answer-button-4').addEventListener('click', checkAnswer);
};
My next question function
function nextQuestion () {
let allAnswers = document.querySelectorAll('.answer-btns');
allAnswers.forEach((o) => {
o.classList.remove('disabled')
});
if (questionNumber >= maxQuestions - 1) {
displayQuizResult();
return;
}
questionNumber++;
clearCorrectAnswer();
clearQuestionTitle();
createQuestion();
clearAnswers();
showCurrentQuestionNumber();
}
Check answer function
function checkAnswer (e) {
let userAnswer = e.target.textContent;
if (userAnswer === questionArray[questionNumber].correct) {
userScore++;
finalScore++;
e.target.classList.add("correct");
showUserScore();
} else {
e.target.classList.add("incorrect");
let correct = document.getElementById('correct-answer');
let correctAnswer = document.createTextNode(`Sorry that's incorrect, the answer is ${questionArray[questionNumber].correct}`);
correct.appendChild(correctAnswer);
}
let allAnswers = document.querySelectorAll('.answer-btns');
allAnswers.forEach((o) => {
o.classList.add('disabled')
});
};