简体   繁体   中英

Javascript Quiz Array Issues

Here's a codepen so you can see what's going on: codepen

I believe I'm having some array issues. If you answer correctly on the last question it doesn't add to the total score and an error pops up in the console. I played with moving around the addScore function but still had no luck, however the "Good Job you got that correct" still shows after the last question.

I also had some text at the very end that showed your final score but that seems to come up on the last question and not AFTER the last question. I'm wondering if the shift() method is messing it all up?

QuestionView.prototype.nextQuestion = function() {
        this.currentQuestion = this.collection.shift();
        this.showQuestion();
        this.handleQuestion();
        this.showScoreAndCount();
}

The error is happening in showQuestion(), which is to be expected considering there are no questions left to show after the last question. It'll count the last answer correctly if you put it in this order

QuestionView.prototype.nextQuestion = function() {
    this.showScoreAndCount();
    this.currentQuestion = this.collection.shift();
    this.showQuestion();
    this.handleQuestion();
}

Since the code stops executing at the error, just putting the counting part before the error will fix that. This way it also increments the Question number to 6/5, but that's a minor thing. Somewhere (or in several places) you need to put a conditional doing different things if it's the last question.

I'm not seeing any text at the end showing your final score, btw. Just the same score count in the corner.

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