I am trying to get a quiz app working with javascript, and I am running into some problems with making the code recognize that the correct answer is being clicked. It just says that there is an error, and that the syntax "correct" is not defined on line 45. I am very new to coding and I still do not fully know what I am doing a lot. In fact, this is my first time using stackoverflow. Here is my javascript:
var quizButton = document.querySelector('#start');
var displayedQuestion = document.querySelector('.quiz-container');
var hideStartScreen = document.querySelector('#start-up-screen');
var questionElement = document.querySelector('.quiz-question');
var answerElement = document.querySelector('.answer-button');
var highScore = [];
var quizQuestions = [{question: "A very useful tool to help with debugging and printing content to the debugger is the: ",
choices: [{text: "Console Log", correct: true}, {text: "Terminal", correct: false}, {text: "Ice Cream", correct: false}, {text: "Javascript", correct: false}],
}];
let shuffleQuestions, currentQuestionIndex;
function startQuiz() {
console.log("Quiz Started!");
hideStartScreen.classList.add('hide');
displayedQuestion.classList.remove('hide');
shuffleQuestions = quizQuestions.sort(() => Math.random () - .5);
currentQuestionIndex = 0;
nextQuestion();
};
function nextQuestion() {
console.log("Next Question");
generateQuestion (shuffleQuestions[currentQuestionIndex]);
};
function generateQuestion (question) {
console.log("Generating Question");
questionElement.innerHTML = question.question;
question.choices.forEach(answer => {
const button = document.createElement('button');
button.innerText = answer.text;
button.classList.add('btn');
if (answer.correct) {
button.dataset.correct = answer.correct
};
button.addEventListener('click', selectAnswer);
answerElement.appendChild(button);
})
}
function selectAnswer () {
console.log('cicked!');
if (quizQuestions.choices.correct == true) {
console.log('Correct!');
} else {
console.log('Wrong!');
}
}
//Start the quiz by pressing the start button
quizButton.addEventListener("click", startQuiz)
Thank you in advance. I hope I was clear enough.
Aucun commentaire:
Enregistrer un commentaire