简体   繁体   中英

JavaScript Quiz getting right and wrong answers not working

I am creating questions in JS, but some of my answers are not showing up because of quotes and trying to escape HTML. I am also having trouble getting the correct user answer to give the user plus points or deductions. Right now, the code is reading every answer as incorrect.

Quiz.prototype.guess = function (answer) {
  if(this.getQuestionIndex().isCorrectAnswer(answer)) {   
    right.innerHTML = "Correct!";
    this.score += 10;
  } else if (!this.getQuestionIndex().isCorrectAnswer(answer)) {
    right.innerHTML = "Incorrect!";
    wrongTimer();
  }
  this.questionIndex++;
}

If user is right, they get 10 points. If user chooses wrong answer, deduct 10 seconds from the timer.

JS Quiz

Within your snippet it turns out that answer is just the actually clicked text.
Since the isCorrectAnswer checks it against the choice key of your question class which is ony "1" , "2" and so on it will never match.

Change your question definition like this and it should work:

new Question(
     "2. What company developed JavaScript?",
     ["Java Inc.",
     "Netscape",
     "JQuery",
     "CERN"],
     "Netscape"
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM