Unable to assign correct answer variable

Good day, Please assist with the following: I'm loading quiz questions from an API and I'm having trouble assigning the correct answer to a variable and accessing and comparing it to the user answer. Below is my Code Pen link. https://codepen.io/l-ree-coh/pen/NWmYMrd Thanks in advance.
2 Replies
Chris Bolson
Chris Bolson3mo ago
You already have the correct answer defined inside the questions.results which is returned from the API. However, if you want to create your own data which may be easier to manipulate, one method would be to push the answers to a new "correct answers" array and then compare them with the answers given by the user. Define array before calling the quiz data: const correctAnswersArr = []; Then within your "quizArray.map((quiz, index) => {" loop:
correctAnswer = quiz.correct_answer;
correctAnswersArr.push(correctAnswer;
...
correctAnswer = quiz.correct_answer;
correctAnswersArr.push(correctAnswer;
...
In your eventlistener function you would need to get the index of the question and compare the given answer value with the corresponding index in correctAnswersArr array.
dirkie#2020
dirkie#20203mo ago
Thank you Chris, that worked for me...!:thumbup: