-1

How do I find the average score between the 2 games in each week?

var scores = {
   week1: {
       game1 : 55,
       game2 : 62,
       average: ?
   },
   week2: {
       game1 : 71,
       game2 : 67
       average: ?
   }
};
Spectric
  • 27,594
  • 6
  • 14
  • 39
illy
  • 41
  • 4

1 Answers1

1

You can use a for... in statement to iterate through the properties of the object and calculate the average individually by summing the scores for game1 and game2 and dividing by 2.

var scores = {
  week1: {
    game1: 55,
    game2: 62,
  },
  week2: {
    game1: 71,
    game2: 67
  }
};

for (const x in scores) scores[x].average = (scores[x].game1 + scores[x].game2) / 2;

console.log(scores);
Spectric
  • 27,594
  • 6
  • 14
  • 39