Iterate Through the Keys of an Object with a for...in Statement 5

Tell us what’s happening:
This has me completely thrown off. It could be my brain just waking up, but I’m stumped badly on this one. What do the instructions mean when it says to use “for…in” in the coding? It has me completely confused, stumped, and thrown off track.

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
  
  // change code above this line
}

console.log(countOnline(users));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 Avast/72.0.1174.122.

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

It is a kind of loop, if it is not enough the challenge text, you can find the detailed documentation here:

I read over the documentation, but never understood one bit of it. My brain is slowly waking up, still.

So, what’s your issue with this kind of loop?

Why you use for-in you can enumerate through objects.
For instance I can call for(a in users) I can then console.log the key (a) or get the value of the key so in this case it would be the age and online properties.

As seen below.

image

So is that the solution to the loop or is that an example? I’m so confused about all this data structure coding, because I’m working on it to ready myself for my new courses on Monday in school.

It just like for loop, but you can’t use for-loop in an object.
So I hope this example make sense:

const obj = {a: 1, b: 2, c: 3};
for (let o in obj) {
  console.log(o)
}
// it will log a, b, c

The variable o in for-in in object refer to the key value of variable obj

I’m sorry. My learning disability is kicking in. It’s been a while today since I’ve been on here to get help, and this loop has me so confused. I’m not sure if I need to put in their names or not, as it confuses me greatly.

Here is an example based on the challenge:

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 key in obj){
   if(obj[key].online){
     count++;
   }
  }
  return count;
  // change code above this line
}

console.log(countOnline(users));

then try use https://repl.it and play with it, like i do here
https://repl.it/@padunk/JS-Playground
:slight_smile:

That example is kinda confusing to my brain. Sorry.

Probably take a step back at this point, understand the fundamentals, what a normal for loop is, how the for…in loops enumerates and the for…of iterates. etc

I don’t even remember where I left off at, though. That’s the thing. I move quickly and have had to use the forums and help pages to get help, yet I still never completely understood it because of my learning disability.

Try to make a step back and try to understand the simpler for loop, otherwise you will not be able to use these things confidently

You know yourself, what do you need? A more hand on approach, someone that explain things to you step by step? Written instructions are better or do you prefer someone talking to you?

I can try to suggest the computer programming course on Khan Academy which has coding walkthroughs and has a different approach and may help you better

What type of coding on Khan Academy? That website has a lot of different coding on it.

The computer programming section, Intro to JS course

That’s the one I’m on, but I’m stuck on one of the challenges of the “What’s for dinner?” one.

You don’t need to do anything fancy there, or you can avoid completing it and go to the next one

You can also avoid challenges and projects altogether and just use the videos to try to understand things better

How do I avoid that challenge? It won’t let me move on to the next part of the lesson unless I complete that challenge. Khan Academy is stubborn for me.

Left part of the page, click on the next thing