Search for a friend


#1


what is wrong with my code? please help.


var friends = new Object();
friends.bill = new Object();
friends.steve = new Object();

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "227-0816",
address: ["Microsoft St., USA."]
}
};

var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "711-1668",
address: ["Apple St., USA"]
}
};

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

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

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


#2

a function ends the moment a return keyword is reached

so, either the first entry of your friends object is a match with what you search, or Invalid is returned ending the function (so won't search through the rest)

you only want to return Invalid after the loop (all people have been searched, none matching the search query)


#3

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