0

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;
};

2 Answers 2

1

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() {
    if (typeof self.rolls[frameIndex + 1] == 'undefined') return self.rolls[frameIndex];
    return self.rolls[frameIndex] + self.rolls[frameIndex + 1];
  }

  function spareBonus() {
    if (typeof self.rolls[frameIndex + 2] == 'undefined') return 0;
    return self.rolls[frameIndex + 2];
  }

  function strikeBonus() {
    if (typeof self.rolls[frameIndex + 2] == 'undefined') return 0;
    return self.rolls[frameIndex + 1] + self.rolls[frameIndex + 2];
  }

  function isStrike() {
    return self.rolls[frameIndex] === 10;
  }

  function isSpare() {
    if (typeof self.rolls[frameIndex + 1] == 'undefined') return false; // cannot be a spare yet
    return self.rolls[frameIndex] + self.rolls[frameIndex + 1] === 10;
  }

  for (var frame = 0; frame < this.currentRoll; frame++) {
    if (isStrike()) {
      score += 10 + strikeBonus();
      frameIndex++;
    } else if (isSpare()) {
      score += 10 + spareBonus();
      frameIndex += 2;
    } else {
      score += sumOfBallsInFrame();
      frameIndex += 2;
    }
  }
  return score;
};

var game = new BowlingGame();

game.roll(5);
console.log(game.score());

Sign up to request clarification or add additional context in comments.

Comments

1

The bug is in the function sumOfBallsInFrame where self.rolls[frameIndex + 1] is trying to access a position in the rolls array that has no value. This makes the result of the function 5 + undefined which becomes NaN because undefined is not a number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.