【发布时间】:2016-01-09 02:29:42
【问题描述】:
具有 12 个元素的关联数组;
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[], 11:[], 12:[]};
获取前 10 个元素之和的最有效方法是什么。下面的代码目前对所有元素求和;
var sum = 0;
for (var k in this.rolls) {
vals = this.rolls[k];
for (var i=0; i<vals.length; i++) {
sum += vals[i] || 0
};
};
this.score = sum
前 10 个元素是:
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[]};
这里是完整的代码:
function Game() {
this.score = 0;
this.frameOver = false;
this.rolls = {1:[], 2:[], 3:[], 4:[], 5:[],
6:[], 7:[], 8:[], 9:[],10:[], 11:[], 12:[]};
this.currentFrame = 1;
// this.lastFrameStrike = false;
// this.lastFrameSpare = false;
// this.isStrike = false
};
Game.prototype.roll = function(pins) {
this.strikeOrSpare(pins);
this.bonusDistributor(pins);
this.rolls[this.currentFrame].push(pins);
this.scoreUpdater(pins);
this.frameHandler(pins);
this.nextFrameBonus(pins)
};
// --------------------------------------------------
Game.prototype.strikeOrSpare = function(pins) {
if (pins === 10) {
this.isStrike = true;
this.frameOver = true
}
else if (this.rolls[this.currentFrame][0] + pins === 10) {
this.isSpare = true;
this.frameOver = true
};
};
// --------------------------------------------------
Game.prototype.bonusDistributor = function(pins) {
if(this.wasSpare) { this.addToLastSpare(pins) };
if(this.wasStrike) { this.addToLast(pins) };
if(this.wasStrike2 && this.currentFrame > 1) { this.addToLastAgain(pins) };
};
// --------------------------------------------------
Game.prototype.addToLast = function(pins) {
this.rolls[this.currentFrame - 1][0] += pins
};
Game.prototype.addToLastAgain = function(pins) {
this.rolls[this.currentFrame - 2][0] += pins
};
Game.prototype.addToLastSpare = function(pins) {
this.rolls[this.currentFrame - 1][1] += pins;
this.wasSpare = false
};
// --------------------------------------------------
Game.prototype.scoreUpdater = function(pins) {
var sum = Object.keys(this.rolls).sort(function (a, b) {
return (+a) - (+b);
}).slice(0, 10).reduce(function (p, c) {
return p + this.rolls[c].reduce(function (p, c) {
return p + c;
}, 0);
}, 0);
};
Game.prototype.frameHandler = function(pins) {
if (this.frameOver) {
this.currentFrame++; this.frameOver = !this.frameOver
} else {
this.frameOver = !this.frameOver;
};
};
Game.prototype.nextFrameBonus = function(pins) {
if (this.isSpare) {
this.wasSpare = true;
this.isSpare = false
if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true
}
} else if (this.isStrike && this.wasStrike) {
this.wasStrike2 = true
} else if (this.isStrike) {
this.isStrike = false;
this.wasStrike = true
} else if (this.wasStrike) {
this.wasStrike = false;
this.wasStrike2 = true
} else if (this.wasStrike2) {
this.wasStrike2 = false
};
};
// --------------------------------------------------
【问题讨论】:
-
前 10 个元素是什么意思?你能说得更具体点吗?
-
this.rolls包含12个元素,需要前10个元素之和 -
我们在谈论一个关联数组(键值对)你期望哪 10 个元素?
-
不保证对象中的属性顺序...
-
您所拥有的对象文字中没有
first 10。要么需要适当的数组,要么为所需的属性键定义标准
标签: javascript arrays loops iterator iteration