Search for a friends 7/8 Help! Lost & Confuse!


#1

Can someone please help with this exercise... about what happen to the else part of the code and what is wrong with my code. Below is my code. Thanks.

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(713) 245-6789",
address: ['One Twenty Four', 'Milford Lane', 'Seattle', 'WA', '98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(713) 245-6789",
address: ['Four', 'Five', 'Nine', 'East', 'Mont', 'View', 'Lane', 'Microsoft', 'WA', '95082']
}
};

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){
cosole.log(friends[keys]);
}
else {
return friends[keys];
}
}
};


#2

There should be no need for an else statement in the search function. The instructions ask us to log the found match, then return it.

Note spelling error... console.

        if (friends[key].firstName === name){
            console.log(friends[key]);
            return friends[key];
        }

Edit: keys => key


#3

I still having trouble with this is exercise. I took out the else part and left return, but I am getting an error "Oops, try again. Did you create a function called search?"

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(713) 245-6789",
address: ['One Twenty Four', 'Milford Lane', 'Seattle', 'WA', '98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(713) 245-6789",
address: ['Four', 'Five', 'Nine', 'East', 'Mont', 'View', 'Lane', 'Microsoft', 'WA', '95082']
}
};

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 === names){
console.log(friends[key]);
return friends[keys];
}
}
};


#4

names => name

Same mistake that I left in the code. Edited now. keys => key


#5

Code that I used that was correct. You don't need the else statement as mentioned above.

var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(715) 123-4567",
address: ['Microsoft Way', 'Redmond', 'WA', '98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(715) 765-4321",
address: ['Paving the Way', 'Cupertino', 'CA', '95014']
};

var list = function(obj) {
for(var cont in obj) {
console.log(cont);
}
};

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