0

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.

8
  • Why are you passing strings to setAnswer? Commented Jun 11, 2020 at 10:21
  • I am doing that so I know which button is pressed - got the idea from another article here! Happy to do it another way if there's a better way of doing it! :) Commented Jun 11, 2020 at 10:23
  • @Sam_987: yes, but why a -string-? Why don’t you just use the plain numbers? Commented Jun 11, 2020 at 10:24
  • Also, the leading comma in your array declarations should be removed. Commented Jun 11, 2020 at 10:25
  • 2
    I would advice against such workarounds, as zero-indexing is very common, it's better to make sure you get comfortable with it. Commented Jun 11, 2020 at 10:33

2 Answers 2

1

q1 is referring to a variable q1 that doesn't exist. If you want the string q1 you need to put it in quotes "q1". So line 2 var questions = ... is throwing an error, and the rest of it isn't finishing execution. You need to fix that line.

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>

Sign up to request clarification or add additional context in comments.

Comments

0
var myQuestions = [
    {
        question: "What is 2+2?",
        answers: {
            a: '3',
            b: '5',
            c: '4'
        },
        correctAnswer: 'b'
    },
    {
        question: "What is blue?",
        answers: {
            a: 'dog',
            b: 'sky',
            c: 'chicken'
        },
        correctAnswer: 'a'
    }
];

You need some sort of better array and to do some sort of loop over it to generate the questions

1 Comment

True but this doesn't directly answer the question posed, and doesn't provide a complete usable alternative. It's a bit of an incomplete answer, maybe more of a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.