对象内部 2 Functions/Methods 中的无限循环
Infinite Loops in 2 Functions/Methods inside an Object
我正在尝试根据下面我的团队对象中的无限循环输出 (a) 个结果。我知道代码在我使用 for 循环自动化 calcAvg
方法之前有效。现在我在这里包含了一个 for 循环,我无法 return 返回一个值,因此,checkWinner
方法没有任何结果。
有人可以帮忙吗?
const team = {
koalas: {
round1: [65, 54, 49],
round2: [23, 34, 27]
},
dolphins: {
round1: [64, 23, 71],
round2: [85, 54, 41]
},
calcAvg: function () {
let d;
let k;
for (let i = 1; i < (this.koalas["round" + i] && this.dolphins["round" + i]); i++) {
k = this.koalas["AvgRound" + i] = this.koalas["round" + i].reduce((a, b) => a + b) / this.koalas["round" + i].length;
d = this.dolphins["AvgRound" + i] = this.dolphins["round" + i].reduce((a, b) => a + b) / this.dolphins["round" + i].length;
console.log(d, k);
}
return [d, k];
},
checkWinner: function () {
for (let i = 1; i < (this.koalas["round" + i].length && i < this.dolphins["round" + i].length); i++) {
if (this.koalas["AvgRound" + i] >= this.dolphins["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Koalas win!`);
} else if (this.dolphins["AvgRound" + i] >= this.koalas["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Dolphins win!`);
} else {
console.log(`Round: ${i}: No one wins, as at least one team needs to score double the score of the other team to win`);
}
}
}
}
team.calcAvg();
team.checkWinner();
这一行中的一个问题:
for (let i = 1; i < (this.koalas["round" + i] && this.dolphins["round" + i]); i++) {
- 应该删除
i < (
(以及结尾的 )
)。这是因为 i
不应与另一个数字进行比较,但条件应检查 "round" + i
是否存在 (已定义)。
所以:
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
您在另一个函数中遇到了同样的问题。
const team = {
koalas: {
round1: [65, 54, 49],
round2: [23, 34, 27]
},
dolphins: {
round1: [64, 23, 71],
round2: [85, 54, 41]
},
calcAvg: function () {
let d;
let k;
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
k = this.koalas["AvgRound" + i] = this.koalas["round" + i].reduce((a, b) => a + b) / this.koalas["round" + i].length;
d = this.dolphins["AvgRound" + i] = this.dolphins["round" + i].reduce((a, b) => a + b) / this.dolphins["round" + i].length;
console.log(d, k);
}
return [d, k];
},
checkWinner: function () {
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
if (this.koalas["AvgRound" + i] >= this.dolphins["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Koalas win!`);
} else if (this.dolphins["AvgRound" + i] >= this.koalas["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Dolphins win!`);
} else {
console.log(`Round: ${i}: No one wins, as at least one team needs to score double the score of the other team to win`);
}
}
}
}
team.calcAvg();
team.checkWinner();
对您的代码的更笼统的评论:定义名称中具有序列号的对象属性是一种反模式。在那种情况下,您应该只定义一个数组而不是普通对象。
其次,由于您将考拉信息与海豚信息相关联每轮,您真的应该将这些数据合并到一个对象中每轮。
所以在你的情况下,你应该更好地组织你的数据:
rounds: [
[
{koalas: 65, dolphins: 64},
{koalas: 54, dolphins: 23},
{koalas: 49, dolphins: 71}
], [
{koalas: 23, dolphins: 85},
{koalas: 34, dolphins: 54},
{koalas: 27, dolphins: 41}
]
]
显然,这意味着您的代码需要进行大量更改。
我正在尝试根据下面我的团队对象中的无限循环输出 (a) 个结果。我知道代码在我使用 for 循环自动化 calcAvg
方法之前有效。现在我在这里包含了一个 for 循环,我无法 return 返回一个值,因此,checkWinner
方法没有任何结果。
有人可以帮忙吗?
const team = {
koalas: {
round1: [65, 54, 49],
round2: [23, 34, 27]
},
dolphins: {
round1: [64, 23, 71],
round2: [85, 54, 41]
},
calcAvg: function () {
let d;
let k;
for (let i = 1; i < (this.koalas["round" + i] && this.dolphins["round" + i]); i++) {
k = this.koalas["AvgRound" + i] = this.koalas["round" + i].reduce((a, b) => a + b) / this.koalas["round" + i].length;
d = this.dolphins["AvgRound" + i] = this.dolphins["round" + i].reduce((a, b) => a + b) / this.dolphins["round" + i].length;
console.log(d, k);
}
return [d, k];
},
checkWinner: function () {
for (let i = 1; i < (this.koalas["round" + i].length && i < this.dolphins["round" + i].length); i++) {
if (this.koalas["AvgRound" + i] >= this.dolphins["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Koalas win!`);
} else if (this.dolphins["AvgRound" + i] >= this.koalas["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Dolphins win!`);
} else {
console.log(`Round: ${i}: No one wins, as at least one team needs to score double the score of the other team to win`);
}
}
}
}
team.calcAvg();
team.checkWinner();
这一行中的一个问题:
for (let i = 1; i < (this.koalas["round" + i] && this.dolphins["round" + i]); i++) {
- 应该删除
i < (
(以及结尾的)
)。这是因为i
不应与另一个数字进行比较,但条件应检查"round" + i
是否存在 (已定义)。
所以:
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
您在另一个函数中遇到了同样的问题。
const team = {
koalas: {
round1: [65, 54, 49],
round2: [23, 34, 27]
},
dolphins: {
round1: [64, 23, 71],
round2: [85, 54, 41]
},
calcAvg: function () {
let d;
let k;
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
k = this.koalas["AvgRound" + i] = this.koalas["round" + i].reduce((a, b) => a + b) / this.koalas["round" + i].length;
d = this.dolphins["AvgRound" + i] = this.dolphins["round" + i].reduce((a, b) => a + b) / this.dolphins["round" + i].length;
console.log(d, k);
}
return [d, k];
},
checkWinner: function () {
for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
if (this.koalas["AvgRound" + i] >= this.dolphins["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Koalas win!`);
} else if (this.dolphins["AvgRound" + i] >= this.koalas["AvgRound" + i] * 2) {
console.log(`Round: ${i}: Dolphins win!`);
} else {
console.log(`Round: ${i}: No one wins, as at least one team needs to score double the score of the other team to win`);
}
}
}
}
team.calcAvg();
team.checkWinner();
对您的代码的更笼统的评论:定义名称中具有序列号的对象属性是一种反模式。在那种情况下,您应该只定义一个数组而不是普通对象。
其次,由于您将考拉信息与海豚信息相关联每轮,您真的应该将这些数据合并到一个对象中每轮。 所以在你的情况下,你应该更好地组织你的数据:
rounds: [
[
{koalas: 65, dolphins: 64},
{koalas: 54, dolphins: 23},
{koalas: 49, dolphins: 71}
], [
{koalas: 23, dolphins: 85},
{koalas: 34, dolphins: 54},
{koalas: 27, dolphins: 41}
]
]
显然,这意味着您的代码需要进行大量更改。