Why does my FOR IN loop, only access the first object? [SOLVED]

Why does my FOR IN loop, only access the first object? [SOLVED]
0.0 0

#1

Can anyone help me understand what is happening here?

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
for (let x in obj) {
    return x
  }

  // change code above this line
}

console.log(countOnline(users));

The result is only Alan But in the instruction in FCC, it’s logs Alan, Jeff ,Sarah ,Ryan

Please help me how I will iterate through this object.


#2

becasue of return dear.

return means the method needs to return to invoker. in your for look, when it tries to loop over all elements, at the first element it returns and rest of the look and method is ignored.


#3

What should I use instead of return? so that I can loop through all the objects?


#4

(I really don’t know what does your assignment asked? you may please share the link)

You may log the for-each element x instead of return it.

Also you may not return anything from function, and you should not expecting anything from the function to log.

So comment/remove the current console.log(countOnline(users)); and just call the function as countOnline(users).
In function also, log the x in for loop, instead of returning it.

You may also share the link of FCC case-study/assignment so we may help more accurate.

Have a try


#5

I tried console.log(x) and it returns all the names plus an additional undefined.

for (let x in users) {
    console.log(x);
  }

 /* Alan
 Jeff
 Sarah
Ryan
 undefined */

#6

Here is the FCC link :

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/-iterate-through-the-keys-of-an-object-with-a-for---in-statement


#7

The last undefined could be becasue of console.log(countOnline(users));, you sure you remove it out?

This is becasue your function doesn’t return anything now, so the console may log undefined


#8

I didn’t remove it because the FCC challenge says that I should “only change code below and above this line” .

Have you finished this challenge.?


#9

Yes got it,

great for sharing the link, the link asks you to count the elements come with online true, not logging them out!

In the for loop you should count the elements with online with true value, then at the end return the counted value.

Now you may have a total variable before the for with init value of 0. in the for loop try to figure out if the element has online of true, then add one to total and at the end return the total.


#10

Have you finished this challenge?

Please look at my code and tell me what I did wrong please:

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
    
   
for (let x in obj) {
   if (obj[x].online == true){
     return obj[x].online;
   }
  }

  // change code above this line
}

console.log(countOnline(users));`Preformatted text`

#11

Awesome, tech for loop and the if looks correct now. But you don’t count the elements with online true, instead you return the first element with online true.

You declare a zero variable before the loop like var total=0;

Now instead of you return in for look(inside if), you add one to total (total++) when online is true.

At the end (after for loop) you return the counted elements with online true


#12
function countOnline(obj) {
    // change code below this line
    var counter = 0;
    for (var x in obj) {
        if (obj[x]["online"] === true) {
            counter++
        }
        // change code above this line

    } return counter;
}

console.log(countOnline(users));

Thanks dear! I finally did it.


#13

O YES! :+1:

That’s it. My suggestion for you. never rush in coding, always review your code for every statement you do. review code when face with any issue. 99% of the problems are fixed and caused by your own.

keep going on great work dear, happy programming.


#14

@NULL_dev

what is the difference between for(user of obj) and for(user in obj)?

@radrar1

congrats in solving the problem. you’re the man!


#15

Good question. I’m not a js expert, but for in works for emurable types. Mostly arrays, json, and types are used to store another elements.

As I checked for of, it’s working for iterable types. It mostly includes the emurable types like array and json works for for in too. Same some more non-emurable elements like String.

String is known as a non-emurable type, but the truth is it’s an array of characters, so it’s iterable.

Check firefox reference which is almost awesome.

Keep going on great work, happy programming.


#16

For…in will loop over enumerable properties of any javascript object. That means you’ll get it’s prototype properties if they are not set to not be hidden.

For…of will loop over iterable objects in javascript. Think of iterables as objects with items in an index. Like arrays and strings.