Assignment Evaluation Error in Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement

Assignment Error

Hi everyone, I think I might have spotted a bug in the webpage.

I’ve been struggling with the assignment Basic Data Structures: Iterate Through the Keys of an Object with a for…in Statement

It seems odd that my code doesn’t work when I run it on the Chrome Console and it gives me the expected output.

Code


function countOnline(usersObj) {
// change code below this line
usersCounter = 0;
for(let user in usersObj)
{
  
  if(usersObj[user].online)
  {
    usersCounter++;
  }
  
}
return usersCounter;
// change code above this line
}

Furthermore, whenever I copy the solution provided by FCC, which is the following:

Code

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};
function countOnline(obj) {
  // change code below this line
  let result = 0;
  for (let user in obj) {
    if (obj[user].online === true) {
      result++;
    }
  }
  return result;
  // change code above this line
}
console.log(countOnline(users));

Browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Iterate Through the Keys of an Object with a for…in Statement

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for...in-statement

I have already refreshed the browser page and even closed and reopened the page.

Is something else I’m missing?

Thank you all for your help,

Togeri.

Use var or let to scope the usersCounter variable. It should work then -

let usersCounter = 0;

1 Like

Damn I can’t get my mind used to ES6. I’m just stupid, hahaha.

Moving this from Contributors to Programming Help.

Your answer has been marked as Solution. Thank you very much @Manish-Giri !

1 Like