循环的Javascript;不起作用(把数字加到总数上)
Javascript for loop doesn't work (adding numbers to a total)
我正在使用Jasmine进行JS测试,不幸的是,我无法通过以下测试。
it('should know the total game score', function() {
frame1 = new Frame;
frame2 = new Frame;
game = new Game;
frame1.score(3, 4);
frame2.score(5, 5);
expect(game.totalScore()).toEqual(17)
});
我得到的错误消息如下:错误:应为0,等于17。
代码如下:
function Game() {
this.scorecard = []
};
Game.prototype.add = function(frame) {
this.scorecard.push(frame)
};
// Why is this not working!!???
Game.prototype.totalScore = function() {
total = 0;
for(i = 0; i < this.scorecard.length; i++)
{
total +=this.scorecard[i].rollOne + this.scorecard[i].rollTwo;
}
return total;
};
function Frame() {};
Frame.prototype.score = function(first_roll, second_roll) {
this.rollOne = first_roll;
this.rollTwo = second_roll;
return this
};
Frame.prototype.isStrike = function() {
return (this.rollOne === 10);
};
Frame.prototype.isSpare = function() {
return (this.rollOne + this.rollTwo === 10) && (this.rollOne !== 10)
};
手动将数字相加似乎有效,例如total=game.scoreca[0].rollOne+this.scoreca[0].rollTwo,但for循环(即使看起来正确)似乎不起作用。如有任何帮助,我们将不胜感激:)
我不太确定,但您似乎没有调用"Add"方法,因此没有数据添加到记分卡中。
我猜你必须将帧添加到你的游戏中
it('should know the total game score', function () {
frame1 = new Frame;
frame2 = new Frame;
game = new Game;
// those lines are missing
game.add(frame1);
game.add(frame2);
frame1.score(3, 4);
frame2.score(5, 5);
expect(17).toEqual(game.totalScore())
});
否则,记分卡数组为空,因此总分等于0。
丢失(因此没有数据添加到记分卡中。)
game.Add(frame1);
game.Add(frame2);
相关文章:
- 使用全局变量来存储数字(JavaScript)
- 字符串到数字JavaScript
- 查找总和最大的数字(Javascript)
- 将字符串中的重复字符替换为不断增长的数字 [Javascript]
- 选择下拉选项时显示数字-javascript
- 用户在数字上输入金额,然后输出最高数字javascript
- 字母数字 JavaScript 正则表达式失败
- 仅数字 Javascript 表单验证
- 如何从一组特定的数字中找到最接近的更高数字:javascript
- 将字符串 num 转换为数字 JavaScript
- 将字符串转换为数字 Javascript
- 提取两种模式之间的数字 javascript
- 添加两个十六进制数字 javascript 与位移
- 确定是否按下了字母或数字-Javascript
- 可以't将字符串转换为数字javascript我的代码出了什么问题
- 按布尔值,然后按数字(javascript)对对象数组进行排序
- 正则表达式,用于获取方括号之间的字符串和数字-javascript
- If和Else比较数字JavaScript
- 检查变量是网页上的所有数字javascript
- 同时检查模为0的两个不同数字(Javascript)