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

Continuing the discussion from freeCodeCamp Challenge Guide: Iterate Through the Keys of an Object with a for…in Statement:

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

So I tried this code as a solution but it doesn’t seem to work. So I googled it and I think I found the mistake. The increment sign (++) is on the wrong side of the variable result.

So I think the correct solution would be to put ++result instead of result++.

Am I correct?

no, that will not make any difference. The solution is correct, even if slighlty outdated (it doesn’t match the current starting code)

2 Likes

Okay, yeah, I can see it works now that I also copied the rest.

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

2 Likes

I have the same problem, the solution did not work until I copied everythin

Yep, this challenge is Broken right now, no Array to get the values from, and the “// Only change bellow this line” kinda implies in not creating one.

it is not broken, you have an example of the possible object in the challenge description, and it will be what’s passed as function argument:

function countOnline(usersObj) {

so usersObj is the object you need to analize

the solution in the guide also works if you use the correct parameter name (the solution uses obj the challenge usersObj)

1 Like

thank you I was confused as well

Hey @ilenia,
I have a question related to this project, but may be more about my syntax in general.
My answer:

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

the actual solution :

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

The only difference between what I wrote and the solution:

Mine:
if (usersObj[user][online] == true)
Solution:
if (usersObj[user].online == true)

Why does dot notation work, but not bracket notation?

1 Like

if you use online like that inside bracket notation, it’s a variable, to use bracket notation you need to write a string literal, "online", with dot notation it works because it accesses properties with the exact name stated

8 Likes

Thank you! This makes total sense; I recall this rule from the objects sections at the end of basic JS and regex sections.
Also, I recognize you’re one of the most active leaders in on the sections I’ve been learning. Just want to recognize this and say thank you for your overall presence and willingness to answer questions. Cheers

1 Like

Here’s how I solved the problem

let users  = {
  Alan: {
    online: true
  },
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

function countOnline(usersObj) {
  // Only change code below this line

let count = 0
for ( let user in usersObj){
  if(usersObj[user].online == true){
      count++
  }
}  
return count
  // Only change code above this line
}
console.log(countOnline(users))

//My advice would be to always use console.log() to check your outputs.
For Instance: 
for ( let user in usersObj){
       console.log(user) 
} output will be [Alan, Jeff, Sarah]
1 Like

I’ve closed this thread.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

2 Likes