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.


#9

hello there,sorry for getting back after long.
my logic is , i am simply iterating an array and checking if object as online property , if it turns out to be true count gets increase else it returns zero.


#11

@alkapwn3d, I really doubt that for ... of loop would want for work with object. For ... in is correct way.

@omkarkothavale88, when you doubt about something I would suggest using your browser’s console to check what’s going on:

let users = {
  Alan: {
    age: 27,
    online: false
  },
  Jeff: {
    age: 32,
    online: true
  },
  Sarah: {
    age: 48,
    online: false
  },
  Ryan: {
    age: 19,
    online: true
  }
};

for (let users in users) {
  console.log(users);
}
// Reference error: you cannot do (let A in A)

for (let user in users) {
  console.log(user);
}
// We get keys of the object (Alan, Jeff...) not values, so user === 'online' won’t work.
// Let’s try to get values:

for (let user in users) {
  console.log(users[user]);
}
// Now you get the access to the values and can check if users[user].online

Now to the other stuff,

  1. return will break your loop and immediately return the outcome. (Always 0 in your case - your app has no users online. Sad!)
  2. You don’t have to compare truthy condition to true, the expression itself is enough:
const t = true;
if (t) return 'Hello World!';

Hope it would help :slight_smile: