7. Search for a Friend - Don't understand


#1

I can pass this exercise, however, I don''t understand this part. Can someone help me please!!!!!!!!!!!!!!! Thanks

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


var friends = {
    
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "14141414",
        address: ["One","Two","Three"]
        },
    steve: {
        firstName: "Steven",
        lastName: "Wong",
        number: "12010101918",
        address: ["four","five","six"]
        }
    
};

var list = function (friends) {
    for (var x in friends) {
        console.log (x);
    }
};

list(friends);


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

search("steve")


#2

the return should also be inside the if clause, since you only want to return your friend once you found it


#3

Thank you. So do you mean like this?

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

Also, technically, I can use "." instead of "[ ]" right? As they should mean the same thing?

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


#4

well, does it work? Then it is most likely right

No, you can not. Because of your for loop:

for(var key in friends)

the friends (bill and steve) are now strings, so you can't do this. if it was the object name you could:

friends.steve.firstName

but thanks to your for loop, it now is a string, so then you get this:

friends."steve".firstName

that doesn't work, since objects names are not suppose to be strings, so you have to use the associative array notation (square brackets)


#5

Ok thanks. I understand why I cannot use "." instead of "[ ]".

Actually, I just tried my code, it is not working if I put the return inside the if clause? Can you take a look for me please?

This is what i got now


var friends = {
    
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "13501758924",
        address: ["One","Two","Three"]
        },
    steve: {
        firstName: "Steven",
        lastName: "Wong",
        number: "12010101918",
        address: ["four","five","six"]
        }
    
};

var list = function (friends) {
    for (var x in friends) {
        console.log (x);
    }
};

list(friends)

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

#6

well, if you are going to change more things, i can't help it, you do it right here:

if(friends[key].firstName

but in your last code, you took out the . between friends[key] and firstName


#7

Even though I have removed the dot in below, my code still doesn't work. It is saying SyntaxError: Unexpected identifier.

if(friends[key].firstName


#8

why is it going so wrong? you should add the dot, then everything should be good. Please post an updated version of your code, preferable with markup:


#9

Ok it works. Also, there is a problem in below, where i have mistakenly written Steve into Steven. Anyway. thanks!

steve: {
firstName: "Steven",