7. Search For Friends


#1

What Am I doing wrong

var friends = {
location: 'California',
date: 'Friday'
};
var friends = new Object();
friends["location"] = "California";
var friends = new Object();
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
var friends = new Object();

friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(101) 222-3303",
adress: ['One Microsoft Way','Redmond','WA','98052']
};

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

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


#2

It asks if i created a search function_


#3

You have:

Uncaught ReferenceError: Steve is not defined(…)

because:

var search = function(name) {
   for(var name in friends) { // you declared name NOT Steve in friends object
      if(friends[Steve].firstName === name) { // here used friends[Steve] 
         console.log(friends[name]);
         return friends[Steve];
      }
   }
};

BUT: for(var name in friends) -> var name is bad variable name because you have name as a parameter of search() function.

So, should be:

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

search("Steve");

#4

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