I have the code below for a ten pin bowling scoring system, and am trying to access the 'score' variable within the console as the score increases. When I create a new game by typing var game = new BowlingGame() I then run the command game.roll(5) to get a score going, however when I then type var myScore = game.score() and then type myScore into the console, I get NaN. I have also just tried typing game.score() but can not retrieve it. Is there anyway of retrieving the updated score as I continue to create new rolls?
var BowlingGame = function() {
this.rolls = [];
this.currentRoll = 0;
};
BowlingGame.prototype.roll = function(pins) {
this.rolls[this.currentRoll++] = pins;
};
BowlingGame.prototype.score = function() {
var score = 0;
var frameIndex = 0;
var self = this;
function sumOfBallsInFrame() {
return self.rolls[frameIndex] + self.rolls[frameIndex + 1];
}
function spareBonus() {
return self.rolls[frameIndex + 2];
}
function strikeBonus() {
return self.rolls[frameIndex + 1] + self.rolls[frameIndex + 2];
}
function isStrike() {
return self.rolls[frameIndex] === 10;
}
function isSpare() {
return self.rolls[frameIndex] + self.rolls[frameIndex + 1] === 10;
}
for (var frame = 0; frame < 10; frame++) {
if (isStrike()) {
score += 10 + strikeBonus();
frameIndex++;
} else if (isSpare()) {
score += 10 + spareBonus();
frameIndex += 2;
} else {
score += sumOfBallsInFrame();
frameIndex += 2;
}
}
return score;
};