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
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.
@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.
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.
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.
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…
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?
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.