I am writing a search function within my Javascript code. However, it keeps on returning search function doesn't return correct contact information.
The goal of this search function is to return the contact information of the object , ie, firstName , lastName , number and address. If given an input Name , it matches one of the keys in the object I created, it should return the contact information and log it on the console.
var friends = {
bill:{
firstName : "Bill",
lastName : "Gates",
number : "(123) 456- 7890",
address : ['Microsoft', 20]
},
steve:{
firstName : "Steve",
lastName : "Jobs",
number : "(123) 456- 7890",
address : ['Apple', 30]
}
};
var friends1 = new Object();
var list = function(friends1){
for(var key in friends1){
console.log(friends1[key].firstName.toLowerCase());
}
};
var search = function(name){
for(var key in friends1){
if(friends1[key].firstName.toLowerCase() === name.toLowerCase()){
console.log(friends1[key]);
return friends1[key];
}
}
};
list(friends1);
search("Steve");
listis irrelevant to the question, it would really be best not to include it in the question.