sdawes
sdawes

Reputation: 661

Accessing one of a number of variables within a Javascript function in console.

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

Upvotes: 0

Views: 81

Answers (2)

WalksAway
WalksAway

Reputation: 2829

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());

Upvotes: 1

Jon Koops
Jon Koops

Reputation: 9291

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.

Upvotes: 1

Related Questions