Why is this saying users not defined?

Tell us what’s happening:

I keep getting “users is not defined” when i console.log this. It’s exactly the solution that was provided.

Your code so far


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


}
console.log(countOnline(users))

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 13020.82.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.112 Safari/537.36.

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

Link to the challenge:

Blockquote

Is obj a global variable? The parameter to the countOnline function is called usersObj, but you’re not using it anywhere in the function body.

That would be my guess, that obj doesn’t have a prop of whatever user is holding. For example:

const obj = {
  prop1: 1,
  prop2: 2,
};
const user = 'prop3';

console.log(obj[user]);
// undefined

As I think is being suggested, maybe you need to use userObj on the fifth line there.

1 Like

Not really sure how to proceed it’s one of those “only change codes beneath this line” ones…and my answer is exactly what the solution is listed as.

How about you try using the usersObj parameter that’s passed to the function?

1 Like

Yeah i guess. The solution has that parameter but it’s above the “only change code beneath this line” line so i didn’t think it was part of the actual solution. I’m sure that will work. Just a deviation from the way everything has been up to now. Thanks.

OK i tried that. Now it’s saying obj is not defined.

please share your most recent version of your code

I’ll try. Just a screenshot?

Copy and pasting the code like it is in the first post is best.


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

let users = {
  Alan: {
    online: false
  }, 
  Jeff: {
    online: true
  },
  Sarah: {
    online: false
  }
}

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


console.log(countOnline(users));

i tried not sure i did it right

here you have users but it is never defined

here you have obj but it was never defined

Just to check your understanding of functions in JavaScript, can you point out what’s wrong with my function below?

function add(x, y) {
  return a + b;
}

add(1, 2);

I want it to take two number inputs and return their sum, but when I run it in the console it gives me the following error.

Uncaught ReferenceError: a is not defined

I’d guess that you’re trying to add two random variables without having them assigned to anything? Wouldn’t something like

return x + y;

work better?

1 Like

Exactly. and that’s basically what is going on with your function.

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

Your function knows about usersObj but you are using obj.

Yeah that was exactly it. Thank you all for the help. Onward and upward. After a huge mug of coffee.

2 Likes

Nice job! I know it can be frustrating when everyone on here gives you hints without actual code, but learning to walk through your program and notice things that are out of place like an unmatched bracket or an undefined variable is a skill that everyone needs to master. And luckily once you start programming in an IDE, there are tools like ESLint that do a lot of that work for us. But for now, in the freeCodeCamp editor, you need to be vigilant about variable names yourself.

@JeremyLT Thanks for wrapping up my train of thought there :+1:

3 Likes

It takes a village. Thanks again.

1 Like