For...in statements help

I am having trouble understanding the solution to this lesson from Basic Data Structures. The solution is here: freeCodeCamp Challenge Guide: Iterate Through the Keys of an Object with a for...in Statement

I don’t understand how the program knows that user refers to the names of the users? I know that it has been defined as a let variable, but I still don’t get how that means it is automatically referring to the names…

9 Likes

Hey @chunzg :wave:,

The statement for...in works by assigning a variable to the value of a property of an object. Here’s a simple explanation:

const object = { 
  a: 1, 
  b: 2,
  c: 3 
};

/*What this is essentially doing is, looping through variable
"object" and assigning it's value into "key". So if you put a console.log inside the for...in loop, it will return every property.*/
for (let key in object) {
  console.log(key);
}
// This loop will log:
// "a"
// "b"
// "c"

So you’re telling the program that user is referring to the value of the names.

Here’s a web docs of for...in statement that might help you:

Hope this helps.

15 Likes

Thank you. I think I get it now.

3 Likes

I AM also having a difficult time with the same challenge concerning the for…in statements. It’s not clear on how to complete what is presented . Even by me following the “solution” exactly it’s still coming back incomplete. Anyone’s assistance with this quandry would be very much so appreciated.

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

It’s what is listed as the Solution.I’m sure it’s just a detail that I’m not seeing but I have yet to find where I’m missing it. Your assistance would be very much so appreciated.

without seeing your code is still not possible to know what’s wrong

And to be even more specific its

let result = 0;
for  (let user in  obj) {
  if (obj[user].online === true) {
       result++;
    }
}
     return  result;

is this all your code?
where is the function definition?


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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

2 Likes

AH ! That could be it . When you say the function definintion do you mean
function countOnline(usersObj)?
my apologies I assumed anyone who’d help was familiar with the exercise in question

1 Like

if that’s your function definition… where does obj come from? where is it defined?

it’s for this that it’s always best to show whole code

You seem like an expert in coding lol…I AM a novice lol

I don’t understand what you mean @ this point. I’m getting even more confused. I shared with you what was presented and then I was supposed to "Only Change the code below this line " etc.

that’s correct, you just need to change code where specified

but where is obj defined?
you are iterating over the properties of the obj object, just there is not a variable of that name in your code

the line for user in obj it’s the first time the obj variable appear but you can’t use variables that never appeared before in your code

1st off allow me to thank you for your time it’s very much so appreciated. And if you clicked on the freeCodeCamp Challenge Guide I pasted in my earlier post today that might give you more of what your asking me to provide. I don’t know does that seem unlikely ?

We still need to see everything in your code editor on the right hand side of the challenge screen to be able to help you debug your problem. It can be very hard to help someone when we can’t see absolutely all of the code.

1 Like

But you have provided more clarity than what I had and it’s appreciated.
Thanx

Is there a way to apply what you’re saying here in this section ?

It really helps if you make postings using the ‘Ask for Help’ button


If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like

This exercise was hard, not because of the problem, but because it felt like the original code was missing some parts necessary to understand. for instance the actual object we are working on is absent, so it’s hard to figure out that we have to use the object called users. Also, the function calls userObj, which was confusing to me.

I would suggest having the actual object written on top of the code let users = { Alan: { age: 27, online: false }, Jeff: { age: 32, online: true }, Sarah: { age: 48, online: false }, Ryan: { age: 19, online: true } }; and at the bottom having countOnline(users);

12 Likes