Whats wrong in my code?

Whats wrong in my code?
0

#1

Tell us what’s happening:

Your code so far


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 count = 0;
  for (let obj in obj){
    if(obj['online'] == true){
        return count++;
    }else{
        return count;
    }
}
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

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

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


#2

You are immediately returning count variable when you find or not find online to be true.

Only increment count variable inside the if statement.

Then before the function ends, return count.


#3

@omkarkothavale88, I don’t know even where to start… let’s go line by line, can you please explain your logic here?

for (let obj in obj) {...

#4

Just a heads up. Being rude to people is not likely to inspire them to want to get better.

Cheers.


#5

@JordanMarsh It is not rude to ask campers seeking help to explain their logic. That is how we figure out what they are thinking to provide the best help possible.


#6

I understand. It’s possible I misinterpreted what he was saying. The “I don’t even know where to start” was off putting.

My apologies.


#7

I have learned to not try and read between lines which may or may not be there. :slight_smile:


#8

What an amazing reply.

@omkarkothavale88, look into what a for…in loop actually does.