8. Victory


#1


  1. Victory!

    The code is only returning "Bill" for some reason instead of giving me the contact information. Please help on how I can get the rest of the information as well!


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "212",
        address: ["68","Wilkie Road","228070"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Plier",
        number: "313",
        address: ["53","Sophia Road","123456"]
        },
        
    Jack: {
        firstName: "Jack",
        lastName: "Skellington",
        number: "111",
        address: ["11","XYZ Road","111111"]
        },
        
    Rose: {
        firstName: "Rose",
        lastName: "Drownsnever",
        number: "222",
        address: ["22","Titanic Road","222222"]
        }
};

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

search: ("Bill")


#2

Hi you should call your function search like that

seacrh("Here type the name ");

#3

Yep, I tried that, but it returns only the the name in quotes, i.e. "Bill" if I type in search ("Bill").

I'm looking to get all the contact information, not just his name, I've changed my question as well. Would you know how to change the code to get the info?


#4

Your serach statetment should be like that

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

instead of

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

your return friends[key] should be inisde the if statement


#5

It isn't changing anything..output is still the same


#6

I think that @leonhard.wettengmx.n Could help you with that.


#7

@codecoder81953,

Please give us the FULL code you are using now....


#8

you have no list function


#9

You need to change keys name Jack and Rose to jack and rose. like:

jack: { }

rose: { }

Best of luck :slight_smile:


#10

Check the first lesson, which tells all the code:

var friends = {};
friends.bill = {
  firstName: "Bill",
  lastName: "Gates",
  number: "(206) 555-5555",
  address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
  firstName: "Steve",
  lastName: "Jobs",
  number: "(408) 555-5555",
  address: ['1 Infinite Loop','Cupertino','CA','95014']
};

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

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

list(friends);
search("Steve");`