Search for a Friend


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#


No error message but why do I get duplicate return of Steve's contact information? Also, in the line of code ' if (friends[key].firstName === name) ', where do we define before what a 'name' is? How does the program know that 'firstName' is a name? Thank you!


Replace this line with your code. 
var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Johnson",
        number: "1234567890",
        address: ['999 hello way','rock','MD', '20854']
    },
    steve: {
        firstName: "Steve",
        lastName: "Jay",
        number: "1234567899",
        address: ['998 hello way','rock','MD', '20854']
        }
};

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

list(friends);

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


#2

I am not sure about the duplicate but to answer your second question: in your function search, the variable name is passed through. The if statement simply checks if the first name of each person in the list of friends is equal to the name variable that you pass through as an argument.


#3

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