I am brand new to JavaScript and struggling with a quiz I am building. I am trying to compare the variable answer which is set by onclick with the correct answer which is an element in the rightAnswers array. There are 4 divs that I am using the set the variable and I've added them below.
All I want is the alert to fire so I know it's working so I can then continue adding to the function and behave as I want.
var qNumber = 1;
var questions = [, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15];
var rightAnswers = [, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3];
var answer = 0;
function setAnswer(choice) {
answer = choice;
checkAnswer();
}
function checkAnswer() {
if (answer == rightAnswers[qNumber]) {
alert("you got it right");
}
}
<div class="answerA" onclick="setAnswer('1')">
<div id="A">Answer 1 goes here</div>
</div>
<div class="answerB" onclick="setAnswer('2')">
<div id="B">Answer 2 goes here</div>
</div>
<div class="answerC" onclick="setAnswer('3')">
<div id="C">Answer 3 goes here</div>
</div>
<div class="answerD" onclick="setAnswer('4')">
<div id="D">Answer 4 goes here</div>
</div>
It seems to be the checkAnswer function that is causing the issue and specifically when I add the reference to qNumber.
Any help would be greatly appreciated. Thank you.
setAnswer?