首页 > 解决方案 > 对象内的 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();

标签: javascriptfunctionfor-loopmethodsinfinite-loop

解决方案


这一行的一个问题:

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

显然,这将意味着您的代码会发生很多变化。


推荐阅读