Iterate Through the Keys of an Object with a for...in Statement, can't solve it

Tell us what’s happening:
Hello everyone, could someone help me how to solve this problem, I can’t figure it out.

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
  for (let users in obj){
    let names = obj[users];
    if (names.online == true) {
      console.log(users);
    }
  }
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 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

What do the failing tests say? What different approaches have you tried to solve the problem? Where do you think the error might be?

The function countOnline returns the number of users with the online property set to true, that’s the only thiing I need to correct.

Your function doesn’t currently return anything. It also doesn’t count the number of users who are online.

It only returns the users whose online property is set to true.

The code you included above does not have a return statement.

Can you show me how to answer it?

You need logic that determines how many users are online and you need to return that vlaue.

function countOnline(obj) {
	let names = "";
	for (let i in obj){
		names = obj[i];
		if (names.online == true) {
			console.log(i);
		}
	}
}

it only returns the name who are online, I can’t figure it how to return the number of users who are online even if I use length.

1 Like

it doesn’t return anything. It may console.log() them, but it doesn’t return them.

First thing, if you want to return a count, maybe you want a count variable – perhaps where you declare let names = "" you may want to also declare count.

Now, your line for (let i in obj){ tells the js interpreter to iterate over obj, retrieve each key (and the keys are ‘Alan’,‘Jeff’,‘Sarah’, and ‘Ryan’), and assign that particular key to i. So far so good.

names = obj[i]; is not strictly necessary – you can then use either names.online or obj[i].online, they refer to the same thing. What’s actually happening here is that i has been assigned to ‘Alan’ for that first pass, so we’re assigning obj['Alan'] to the variable names, giving us all attributes of that nested Alan object.

So, if (names.online == true) { ... } – what do you want to do here? If they are online, this might be the place to increment that count variable.

Now, after you have iterated over all the object keys (or user names), and you’re out of that loop, it would be good to return something. What do you think you might return?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Can you show me sir the possible solution, please? I want to know what the answer is and to understand how it was answered. Link of 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

The whole point of my above post was to answer the question “how it was answered.” I tried to break the process down, step by step, and show you where your code was good, and where something was missing.

The only changes I might suggest you make to your code:

  • define a variable outside the for(…in…) loop to handle the count.
  • Increment the count for each online user.
  • Return the count at the end.

I’m not entirely comfortable outright giving the answer, as IMO that defeats the purpose of the lesson.

Thank you. its really helpfull for me

function countOnline(obj) {

  let name = "";
  let onlineCount = 0;
for(let user in obj){
  name = obj[user];
if(name.online)
onlineCount++;
}
return onlineCount;
  
}
1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

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.

Thank you for understanding.

1 Like

Another great option, if you want to compare solutions, is to take yours to a “sandbox site” and set them up there. Places like repl.it, or jsfiddle, or even codepen are all great places to take your block of code, and set up your own tests against it.

I have used them all, and personally, I really like repl.it the best of those three. It has a number of very useful features:

  • I can set up a “sandbox” specific to the language needs of the challenge (for example, an HTML/CSS setup, or a javascript setup, or a Node.js, or quite a few other languages).
  • Very quick setup and startup.
  • Multiplayer mode – I can create a very quick paired programming session, both to learn new things and/or to tutor others.

I have used that last feature quite a few times, and it is AMAZING. When you find yourself stuck on a coding challenge, setting up a study group around the challenge and getting two, three, or four folks helping to think things through and test them is great. Being able to interactively edit and comment on code is a very powerful tool.

So my point, if you want to share your solutions for others to review or compare, set up a sandbox site. I have set up three different sandboxes for the same challenge sometimes, simply to compare different ways of coding the solutions. It is a far more useful code chunk than on these pages, and helps to keep the forum a LOT less cluttered.

1 Like

Thank you and sorry for my wrong action :slight_smile: i will use them from now. Thank you again :slight_smile:

1 Like