Search function doesn't return contact information for Steve


#1


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

Oops, try again. It looks like your search function doesn't return contact information for Steve.

var friends = {
    
  bill:{
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address:['old street','no:39','jackson apt']
     },
     
 steve:{
      firstName: "Steve",
      lastName: "Jobs",
      number: "(106) 444-4444",
      address:['new street','no:23','home apt']
     }
 
};

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

 var search = function (name) {
     for (var key in friends) {
     if (friends[key].firstName === name) {
      return friends[key];  }
     else {
          return "There is no match.";}
     }
     };
     
     search("Steve")


#2

if (friends[key].firstName === name) {
      return friends[key];  }
     else {
          return "There is no match.";}
     }
     };

return terminates a function whenever a return occurs function ends at that moment.

in your case when key is bill it returns else statement and function ends.
so you can remove the else statement or use console.log if you want to print "There is no match."

next here

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

your parameter and object name are same.change the parameter name and use that in for loop. now call the list function.


#3

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