I'm wondering why I have to use the variable keepScore to make my code works.
After delete var I would write:
questions[n].checkAnswer(parseInt(answer), score())
but then point amount could be only 0 or 1 - var sc=0 execute every cycle.
Then what is the difference between these two codes?
function score() {
var sc = 0;
return function (correct) {
if (correct) sc++;
return sc;
}
}
var keepScore = score(); //here
function nextQuestion() {
var n = Math.floor(Math.random() * questions.length);
questions[n].displayQuestion();
var answer = prompt('Please select the correct answer.');
if (answer !== 'exit') {
questions[n].checkAnswer(parseInt(answer), keepScore); //here
nextQuestion();
}
}