Getting the last values used in a quiz with jQuery

I’m trying to do a dynamic quiz in order to learn Javascript/jQuery/html and CSS. I got stuck because I just added a back button, with this back button the user can go back an see his last answer and also change it, if the last answer was correctly answered it does score--; but if is not answered correctly it doesn’t reset any value, in order to avoid people to get infinite scores. My real problem is that I’m using a lastValue variable to store the last radio button checked so when you go back it can compare, but it just work well if you go back once, then lastValue doesn’t update so the behaviour is weird. This is the piece of code I’m using to compare the values:

function backQuestion(){
    if (number == 9){
        $('#myForm').css('display', 'inline-block');
        $('#question').css('display', 'block');
        $('#next').css('display', 'inline-block');
        $('#score').css('display', 'none');
    }

    if(number > 0){
        number --;
        addQuestionAndAnswers();
        if (lastValue == allQuestions[number].answer){
            score --;
            console.log('your score after going back is: ' + score);
            console.log('the last value is: ' + lastValue);
            console.log('this is the value of number: ' + number);
        }else{
            //lastValue = 
            console.log('your score after going back is: ' + score);
            console.log('the last value is: ' + lastValue);
            console.log('this is the value of number: ' + number);
        }
    }
    

}

I also leave the the js fiddle demo so you can check the rest of the code and variables. While I was writing this I just thought about storing the values of the responses in an array and then just every response answered I can store that value in the array and then when you push the button back you can get that value in order to compare, then you can just delete that value if the answer is answered again. I’m not sure if this is a complicated way; I’ll appreciate if someone can tell me or suggest me an easy way!

js fiddle: http://jsfiddle.net/xtatanx/Wn8Qg/2/

js fiddle result full screen: http://jsfiddle.net/xtatanx/Wn8Qg/2/embedded/result/

115 thoughts on “Getting the last values used in a quiz with jQuery”

Leave a Comment