Search for a friend - need help


#1


Basically, I think it works, it prints bill and steve but also doesn't complete as I get the error, ReferenceError: key is not defined

do i need to change firstName to key?

var friends = {
    
    bill: {
        firstName:"Bill",
        lastName:"Gates",
        number: "04568888",
        address: ['71 Moorskys','pooperdown', 'newcastle', 'NE1 5TG']
        },
    steve: {
        firstName:"Steve",
        lastName:"Bates",
        number: "04567888",
        address: ['75 Moorskys','pooperdown', 'newcastle', 'NE1 5TG']
        }
};
var list = function(friends) {
    for(var firstName in friends){
        console.log(firstName);
    }
}
  var search = function(friends) {
      for(var firstName in friends){ 
          if(friends[key].firstName === name) {
              console.log(friends[firstName]);
              return friends[firstName];
          }
      }

};
list(friends);
search("search");


#2

changed it all to key from firstName, now I get Oops, try again.
It looks like your search function doesn't return contact information for Steve.


#3

Sorted

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

was the corrected code, i was using friends as to many objects.


#4

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