Search for a Friend: Can't return Steve!


#1

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

I can't get my search function to find Steve. I also added a search("Steve"); but that doesn't find him either.

var friends = {
  
  bill: {
      firstName: "Bill",
      lastName: "Gates",
      number: "804-911-4200",
      address: ["69 Swaggster Road","Dope","Alaska","42069"]
  },
  
  steve: {
      firstName: "Steve",
      lastName: "Jobs",
      number: "804-556-6969",
      address: ["420 GangsterOG Lane","Gohome","Ohio","69420"]
  }
};

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

//search engine!
var search = function (name) {
    for (var firstName in friends) {
       if(friends[firstName].firstName === name) {
           console.log(friends[firstName]);
           return friends[firstName];
       }
       else {
           return("error!");
       }
    }
};

list(friends);
search("steve");


#2

Hi try to remove teh else statement

else {
    return("error!");
}

#3

Search for a Friend: Can’t return Steve!

var jokeMode = true;
var steve = false
var death = true
while(steve) {
   if(death == true) {
      return false
   }
jokeMode = false;
};

:smiley:


#4

Your code looks a little confusing. Always be sure to debug it with console.log statements to see how the output looks like, in my case I used a different method and debugged it, Here's how the code looks like

var friends = new Object(); \first object
{

friends.bill = new Object(); \\bill object
{
    friends.bill.firstName = 'Bill';
    friends.bill.lastName = 'Gates';
    friends.bill.number = "123456667';
    friends.bill.address = ['abc place', 'http://kontent.co.in']; //just some random stuff.
};

friends.steve = new Object(); \\steve object
{
    friends.steve.firstName = 'Steve';
    friends.steve.lastName = 'Jobs';
    friends.steve.number = '869741';
    friends.steve.address = ['india', 'usa'];
};

};

var list = function (friends) \function to print out the names
{
for (var key in friends)
{
console.log(key);
};
};

var search = function(name) \a function that goes through both the names
{
for (var key in friends)
{
if (friends[key].firstName === name)
{
console.log(friends[key]);
return friends[key];
};

};

};


#5

This is my new code and now I'm getting - SyntaxError: Unexpected token and I can't figure out what that is. Can someone please help me?

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "206 555-5555",
address: ["One Microsoft Way","Redmond","WA","98052"],
},
steve: {
firstName: "Steve",
lastName: "Martin",
number: "302 555-5445",
address: ["725 Vandever Avenue","Los Angeles","CA","10276"],
},
helen: {
firstName: "Helen",
lastName: "Featherer",
number: "813 347-3523",
address: ["13906 Henson Circle","Tampa","FL","33625-}"],
};
var list = function(friends) {
for(var x in friends) {
console.log(x);

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

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