7/8 I don't get this at all. Can someone post the answer so I can study it?


#1

No matter what I do I don't get the for/in loop. Can someone post the answer so I can study it and understand what the question is asking?


#2

Apparently this works. I just copied the hint example and I'm studying it now. I came here to see if anyone got a better understanding because I would like to know why it worked before I move on
var search = function(name) {
for(var key in friends)
if(friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
}
search("put name here")


#3

I'm kind of unsure myself, since I just made it past this..

I can say what I think it does though.

This is my "friends" object:

var friends = {
    bill: {
        firstName: 'Bill',
        lastName: 'Hillsbrad',
        number: '58393928',
        address: ['One Tumbleroad Way', 'Kansas', 'WA', '09539']
        },
    steve: {
        firstName: 'Steve',
        lastName: 'Billson',
        number: '99993822',
        address: ['One Superfluous Streetroad', 'London', '4738'] 
        }
};

This is my for/in loop:

var search = function(name) {
    for (var embl in friends) {
        if (friends[embl].firstName === name) {
            console.log(friends[embl]);
            return friends[embl];
        }
    }
}

So my understanding is this:

for (var embl in friends)

this loops through all the keys in the object, i.e. bill and steve.

 if (friends[embl].firstName === name)

So for this code above [embl] is bill, and then, if the firstName property of the key 'bill' matches the name you input in the search function it will continue.

Then if it was a match this:

 console.log(friends[embl]);

prints out everything the 'bill' key contains.

Granted I'm not sure and it might've passed it on a fluke, but meh.


#4

You are absolutely correct.
The for...in loops take a bit of getting used to.
Consider the embl in your example like the counter i in a normal for-loop. It will change each time through the loop, counting the next key in friends. in your current example, the keys is bill and steve.