For...in statements help

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 (’).

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

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);

5 Likes

The issue with the solution is that it says the object is called obj, whereas the starting code in the exercise calls it usersObj. The solution is also needlessly complex because of this issue. Hopefully that acts as a good hint as to how the real solution should be written. If not, I’ve broken it down further below:

The solution says 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));

The actual solution only needs to be this:

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

As expected of an exercise, the only code that needs to be written is indeed between the //Only change code below/above this line directions.

4 Likes

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

Hi,
You will need to change the line ’ function countOnline(userObj)’ to ‘function countOnline(obj)’. So it will work with the listed solution.

Hi, I deeply apologize if this may have been mentioned before in this thread but why did the dot notation not work for the exercise but brackets did?

1 Like

Hi and welcome to the forum!

I’d look at the answer here: Bug in question - does not match solution

1 Like

Oohh, I see. Thank you so much JeremyLT!!! :slight_smile:

1 Like

thank you, this challenge was more confusing then it needed to be. sooo thank you for just giving solution so i could compare me code and find where i was not understanding.

1 Like