How can I solve this for...in?

Hi, I’m trying to solve this one, but I don’t know where to go with it. I don’t like how the entire code isn’t in the code block and how I’m referring to properties online, user when it’s elsewhere. Not sure why we wouldn’t just have it in the code. Even the example is missing the function.

  • I noticed the hint uses obj not usersObj, why is that?
  • Read somewhere I should console.log to find the problem, I still don’t know how to use console.log effectively to debug (tried replacing count++ with console.log(user); but nothin happened)

Thanks

Your code so far


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

Your browser information:

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

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

Link to the challenge:

about debugging, here are the steps I do to see what’s wrong:

I first run the tests, take the first failing test, in this case this one:

The function countOnline should return 1 when the object { Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } } is passed to it

So I take that function call, in this case countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }), and put it in a console.log as last line in the editor

so I create a new empty line and write

console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }))

now, there is an error appearing in the console:

ReferenceError: count is not defined

(note, the error appears with or without console.log, the console.log was to see what’s the otput of the function, which is not showed because it is stopped prematurely by the error)

can you fix that on your own?
try fixing that and other issues you find, post your updated code if you get stuck again


it can be a residual from an old version fo the challene, but it doesn’t change anything as as long it is used correctly, a parameter can have any name

1 Like

You have a few issues here, mainly with count. First of all, you need to declare and initialize that. Then, you need to return that from the function.

  • I noticed the hint uses obj not usersObj, why is that?

Maybe it was written with a different version of the problem.

Read somewhere I should console.log to find the problem,

Things sent to the console will show up there. You can open it with CTRL-SHFT-I or something else, depending on the browser and OS. There is also a “fake” console in the FCC testing program, scroll to the bottom of the test readout. Just look for youtube videos on the developer console. This is a very useful tool - you should learn it. The console is only the beginning.

1 Like

@ilenia @kevinSmith thanks. I initialized var count

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

console.log(countOnline({ Alan: { online: false }, Jeff: { online: true }, Sarah: { online: false } }))
 

But now getting undefined in the console.log. Do I need to console.log something else somewhere else in the code? The part about I also have more trouble with is console.logging in the middle of the code, knowing where that is etc

**update I peeked someone elses answer and saw I need to return count but not passing all yet.

var count = [];

Why is it an array?

And you also need to return it.

what is it that you should return?

But now getting undefined in the console.log.

Right, agreeing with ieahleen, but clarifying…

You are logging to the console, the return value of that function. But that function doesn’t return anything (it should, but it isn’t yet), so, the return value is undefined.

@kevinSmith

What do you mean? Does it matter whether or not I use [] or ""?
I just read the initialize varables section here: JavaScript Best Practices

@ilenia I should return zero for { Alan: { online: false }, Jeff: { online: false }, Sarah: { online: false } } but returning []

I would expect count to be a number, not an array or a string.

Oh I see. That solves it. Is it best practice to initialize a relevant value since we’re lookin to return the number of users?

try console.log([] + 1) and see what it prints

and then see console.log([] + 1 +1)

1 Like

Yes, that is best practice. In some languages it is much more strict and it is more complicated to switch data types. JS is much more loose, but it’s still not a good idea to flip around, especially since there is no reason. A count should be a number.

1 Like

I ended up switching it to var count = 0;

I’m getting 1 and 11. What do you mean by those console.logs?

welcome to the woundrous world of type coercion!

  • the ++ convert what it 's used on to a number, so you are not having issues, but if you need to sum something using a different method…
  • + is an operator for numbers (sum) or for strings (concatenation)
  • [] is neither of those, it is coerced to a string, and writing [] + 1 is like writing "" + 1, which results in "1", now everything you try to add is concatenated instead, "1" + 1 makes "11"

in short, it is not only best practice, it is for your own and everyone else sanity that you should initialize a variable to the data type you want it to use
otherwise you risk unexpected behaviour, or stuff just will not work

1 Like

Gotcha. Thank you! This one shouldn’t be too had to remember :ok_hand:

also,

this was happening because you initialized to a different data type than the wanted output

One of the reasons that TypeScript has become popular is that it is an extension of JavaScript, but with much stricter typing. But you don’t have to worry about that yet - just try to be thoughtful of how you use types and try to to convert back and forth willy-nilly.

Sounds good. Shouldn’t be too hard to remember this one, vs how to add, modify, and remove key-value pairs, check if keys exist, and iterate over all the keys in an object. Which is apparently what I just learned :sweat_smile:

Initializing a variable to an array and then doing ++ on it suggests some issues with the fundamentals. What is it you expected to happen when using the ++ operator on an empty array?

Secondly, in order for ++ to work, the variable has to have a value, otherwise, you are trying to add undefined to undefined.

var test;
test++
// NaN
undefined + undefined
// NaN

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.