-1

I have a little problem with a test. Can you please help me and tell me what's wrong.

Write a for-in loop to loop through each gamer in the gamerScores object. Inside the loop, use the keys and values from the gamerScores object to log the strings pictured above to the console.

let gamerScores = {
  john: 89,
  paul: 725,
  george: 553,
  robert: 9302,
  steve: 733,
};

for (let i = 0; i < gamerScores.length; i++) {
  console.log(gamerScores[i]);
}
Andy
  • 61,948
  • 13
  • 68
  • 95
5undel
  • 11
  • 2
  • You may find this useful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries – Pascal Bugnion Sep 12 '21 at 07:13
  • 1
    A `for..in` loop is a special `for` syntax, which lets you iterate over object properties (the use of `for..in` is often discouraged, but this test asks for using it...). You tried to iterate over it with a regular `for`, but that doesn't work, as the object you're trying to iterate has neither `length` nor index properties. – FZs Sep 12 '21 at 07:34
  • Please refer https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – Rinshan Kolayil Sep 12 '21 at 07:54

1 Answers1

0

Here's the MDN documentation on loops and iteration which you might find useful.

let gamerScores = {
  john: 89,
  paul: 725,
  george: 553,
  robert: 9302,
  steve: 733,
};

for (let key in gamerScores) {
  console.log(`${key}: ${gamerScores[key]}`);
}
Andy
  • 61,948
  • 13
  • 68
  • 95