javascript - 对象内的 2 个函数/方法中的无限循环
问题描述
我正在尝试根据下面我的团队对象中的无限循环输出(a)结果。我知道代码在我calcAvg
使用 for 循环自动化方法之前就可以工作。现在我在这里包含了一个 for 循环,我无法返回一个值,因此,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}
]
]
显然,这将意味着您的代码会发生很多变化。
推荐阅读
- android - PDF 未在 URL 的 WebView 中打开
- javascript - json函数不调用在mvc中使用jquery ajax
- html - 将 div 元素相互定位
- python - 使用用户输入的海龟绘图程序:窗口消失。我究竟做错了什么?
- r - 在 R 包中包含 Shiny 应用程序:传输输入参数
- javascript - React props:我应该传递对象还是它的属性?它有很大的不同吗?
- wpf - 如何减少 WPF 上下文菜单项的内容长度
- javascript - 在 Angular 2+ 应用程序中从 Twitter 获取用户访问令牌
- java - 有没有办法在不先创建 PDF 文件的情况下以 PDF 格式查看 rptdesign?
- regex - SSIS中的正则表达式