2016-06-26 32 views
0

我有一个十针保龄球评分系统的代码,并试图访问控制台内的'分数'变量,因为分数增加。当我通过输入var game = new BowlingGame()创建新游戏时,我运行命令game.roll(5)即可获得分数,但是当我输入var myScore = game.score(),然后在控制台中键入myScore时,我会得到NaN。我也刚刚尝试输入game.score(),但无法检索它。无论如何,当我继续创建新卷时,检索更新的分数?在控制台中访问Javascript函数中的多个变量之一。

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

回答

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

1

该缺陷是在功能sumOfBallsInFrame其中self.rolls[frameIndex + 1]正试图访问的rolls阵列不具有任何值的位置。这使得函数5 + undefined的结果变成NaN,因为undefined不是一个数字。

相关问题