For...in Statement to Iterate Through the Keys of an Object

For...in Statement to Iterate Through the Keys of an Object
0

Tell us what’s happening:
How can I make this work? why “count” looks lighter in “let count = 0;” but not in “count++”? thanks

Your code so far


function countOnline(usersObj) {
// Only change code below this line
for (let user in usersObj) {
let count = 0;
let areOnline = usersObj[user][online].hasOwnProperty('true');
if (areOnline == true){  
  count++;
  
}
}
return count;
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

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

Link to the challenge:

Hey @carmen_zavala, what do you mean by the count is lighter than the other one? Do you mean the font-weight/boldness?

1 Like

Yes. but I don’t get why

This is the message you would get hovering the variable if we didn’t disable the hover message.

‘count’ is declared but its value is never read.

It happens because you have declared the count variable inside the for loop, which scopes it to the loop (only accessible inside the loop). Not only do you not have access to the count variable outside the loop (for the return), it is also getting redeclared on each loop iteration (set back to 0).

BTW, online is not correctly used (it should be a string, not an identifier).

Edit:

You may also want to try logging the “online check”, to see if it is giving you what you expect.

console.log(areOnline)

3 Likes

I’m sorry, but for...in should almost never be used to iterate an object.

Why? Because it includes prototype properties.

@kerafyrm, while you may be correct, this is not sanguine to helping the OP solve their problem. Perhaps you’d like to submit a GitHub issue with your recommendation to remove this from the curriculum if you feel strongly about this.

These OP questions are in a forum and not in a private message. I don’t see the harm in commenting in best practices. The OP got their answer and other programmers both beginner and intermediate come here to learn. If my comments are not beneficial, I can gladly take my advice to a different site.

1 Like

When a FCC challenge asks the OP solve the challenges a specific way, adding a comment telling the user not to use the required technique is not very helpful in building their understanding on how to solve the challenge.

Discussions about curriculum contents are valuable, but they are a better fit for a separate thread or on the FCC’s GitHub where the curriculum development occurs.

3 Likes

The language has a for...in loop. The challenge is teaching it, I don’t see the issue here?

The example code probably should show the use of hasOwnProperty when looping the object (might be worth an update).

Some information about what gets looped might be nice. However at this point in the curriculum (before the OOP challenges) talking about inherited properties may just be confusing. And giving this information in a short, terse yet well-explained sentence is likely difficult.

2 Likes

Yes JS has it., but just because it exists doesnt mean it should be used or taught to be used to iterate objects. This is just teaching it the wrong way.

If you want to iterate the keys, then use Object.keys().

for...in can give false positives.

For example, if a project had Object.prototype.lasjorg = function(u){ return 'blah'; } you would have a problem…

The challenge that comes right after this one is on using Object.keys().

Anyway, we are not going to argue about this in an ask for help thread. If you want to open an issue on GitHub you are more than welcome to do so.

thank u! your comment was really helpful. I already looked for more documentation on dot and bracket notation and also on hasOwnProperty :slight_smile:

2 Likes

My pleasure. If you need any more help, just let us know.

Happy coding!

2 Likes

Hi Jeremy,

I find kerafyrm’s comment to be more beneficial here than if it were on another post. I’m here to soak up whatever info I can when it is most fresh on my mind. TBH, I didn’t understand the comment but made a mental note that the technique used to solve it MAY not be the best. I am not discouraged by it because I then googled “Prototype Properties” and asked myself why they would have issued that caveat…because a prototype prop would be specific to the object? and unless I know what I’m looking for I won’t know the variable is called “user/s” ? Either way I found it germane to the subject.

I’m glad the comment helped you. It’s still best on the forum to focus on helping a user solve challenges in the way required by the challenge instead of making a post telling the user not to solve the challenge in the way required by the challenge. The mentioned technique is covered in the next lesson.

I see your point. I find most comments helpful…BTW I’m still very confused/frustrated and have NO IDEA how this forum works. there used to be a comments section under every “Hint”. The solution to this problem doesn’t look like the above solution and doesn’t work in the FCC code editor. Can you offer any suggestions on how to ask/read up on these?

If you change the variable names to match the variable names in the starter code, then the posted solution should work fine.

this is what I have:

function countOnline(usersObj) {

  // Only change code below this line

  // Only change code above this line

}

Solution is this...
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 result = 0;
  for (let user in obj) {
    if (obj[user].online === true) {
      result++;
    }
  }
  return result;
  // change code above this line
}
console.log(countOnline(users));

I wrote my own answer and when it didn’t work I copied the hint. Still didnt work then I changed my variables to match. Still nothing. I may be in the wrong forum or wrong problem. I searched this thread by pasting the name of the problem.

You’re declaring a obj and counting the users online only for that obj. What you should be doing is looking at the input usersObj and counting how many of them are online.

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.

Please 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.


You have the function definition twice, you need to remove the first part of what you posted up until the object declaration.

Edit: wait, why is it even passing with the object in the code when you are not supposed to have it in the code? It fails without it.

Edit2: OK, I get it, the console.log was referencing a users variable which doesn’t exist unless you include the object.

1 Like