1

I am creating a quiz using OOP in jQuery. But I'm stuck on this problem:

 validateAnswer: function(val) {
        "use strict";
        var userAnswer = [];
        userAnswer.push(val);
        return function() {
            return userAnswer.pop();
        }
    },
    addUserAnswer: function() {
        "use strict";
        var answer = this.validateAnswer();
        var realAnswer = answer();
        quizModel.userAnswers.push(realAnswer);
    }

The val parameter is an integer that represents the value of the answer. For example the first question:

<input type="radio" class="name1" value="1">

On every click on the radio button the validate answer is called. I push all the answers on the question in the array (to account for people that change their mind). I only need the last answer though. So when they click the "next" button i want to collect the last answer given and push it to a global array which contains all the useranswers (used for checking answers and when people want to go back to previous questions). But I always get undefined returned. This is my last try using closures. How to fix this problem?

idontknow
  • 646
  • 6
  • 21

0 Answers0