Search code examples
javascriptfunctionfor-loopmethodsinfinite-loop

Infinite Loops in 2 Functions/Methods inside an Object


I am trying to output (a) result(s) based on an infinite number of loops in my team object below. I know that the code worked before I automated calcAvg method with a for-loop. Now that I've included a for-loop here, I am not able to return a value back, and therefore, nothing comes out of checkWinner method.

Could someone please help?

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();


Solution

  • One issue in this line:

    for (let i = 1; i < (this.koalas["round" + i] && this.dolphins["round" + i]); i++) {
        
    
    • The i < ( should be removed (and the closing )). This is because i should not be compared with another number, but the condition should check that the "round" + i exists (is defined).

    So:

    for (let i = 1; this.koalas["round" + i] && this.dolphins["round" + i]; i++) {
    

    You have the same issue in the other function.

    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();

    A more general remark on your code: it is an antipattern to define object properties that have an sequential number in their name. In that case you should just define an array instead of a plain object.

    Secondly, since you relate koala info with dolphin info per round, you should really combine those data in one object per round. So in your case you should better organise your data like this:

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

    Obviously that will mean a lot of changes in your code.