7. Search for a friend


#1

var friends = {
bill: {
firstName: 'bill',
lastName: 'kelly',
number: '333-333-3333',
address: ['123 Threeway Way', 'Seattle', 'WA', '83221']
},
steve: {
firstName: 'steve',
lastName: 'smith',
number: '666-666-6666',
address: ['123 Deep Throat', 'Las Angelas', 'CA', '90210']
}
}

var list = function(friends){
for (var firstName in friends) {
console.log("Hi, my name is " + firstName);
}
};

var search = function() {
for ( var firstName in friends) {
if (friends.firstName === steve){
console.log(friend.firstName);
return friend.FirstName;
}
}
};
list(friends)
search(steve)

ReferenceError: steve is not defined. WHY??????


#2

Hi @jewpanese

Replace it in your code :

var search = function(name){ //put "name" when you'll call search function it'll replace it by the firstName what you want
    for(var key in friends){
        if(friends[key].firstName === name){ //and search function will check if it's in your friends object
            console.log(friends[key].firstName);
            //you can use also "return friends[key].firstName;"
        }
    }
};

search("steve") //"name" in your function is replace by "steve" and it works

Hold on :wink:


#3

ReferenceError: steve is not defined. WHY?

Because it isn't. friends.steve is. Now let's examine your code...

var search = function() {
for ( var firstName in friends) {
    if (friends.firstName === steve){
        console.log(friend.firstName);
    return friend.FirstName;
    }
}
};

First of all, the function doesn't have a defined formal parameter, obscuring any call arguments. Since we want to have a re-usable function, we will need to have at least a search term, like a first name.

var search = function (name) {

};
search("Steve");

The function is reaching to global scope for the friends object, so we can query it with a for..in statement. It's arbitrary the variable we use, but I like to go with a generic name, rather than one that exists in the property list of the object we are iterating.

    for (var key in friends) {

    }

Once we begin comparing each firstName property in the friends object we will need to treat the object as an array and use subscript notation.

        if (friends[key].firstName === name) {

        }

Finally we want to return the object within which we found the match...

            console.log(friends[key]);
            return friends[key];

#4

I got it to accept the code, but I do not understand why it logged Steve to the console 4 times?

Hi, my name is bill
Hi, my name is Steve
Steve //Why did Steve get logged 4 time?
Steve
Steve
Steve
{"firstName":"Steve","lastName":"smith","number":"666-666-6666","address":["123 Deep Throat","Las Angelas","CA","90210"]}


#5

This is due to the code being run by the SCT to test it. That produces extra output. Also consider that we are both logging and returning an object. The console responds by echoing the last value in its buffer at completion. As long as the output you expect is at the top of the heap, ignore all the rest.