Currently immersing myself in the world of JavaScript and taking on a challenging exercise. The task at hand involves creating a simulation game, with an object containing three arrays (representing each individual game).
Successfully managed to display the average score of the first game in the console, but facing difficulties when trying to do the same for the other two games. Despite setting the 'totalScore' variable to 0, the counting continues from the previous game's total.
For instance, the average score for the first game is 97.66. Ideally, I would want the average calculation for the second game to start fresh from 0, but it seems to carry over from the previous calculation. Any suggestions or alternative approaches to tackle this issue? Appreciate any insights 🙏🏽.
const dolphins = [
[96, 88, 109], //Game1
[80, 76, 120], //Game2
[99, 100, 97] //Game3
]
let score = 0;
for (let i = 0; i < dolphins.length; i++) {
let totalScore = 0; // Should it go out of the loop?
const game = dolphins[i];
console.log(`Game #${i + 1}`)
for (let j = 0; j < game.length; j++) {
score += game[j];
totalScore = score / dolphins.length;
}
console.log(totalScore);
}