7. Search for a friend, why does it print steve 2x and bill 1x


#1



var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
friends.julian = new Object();

friends.bill.firstName = "Bill"
friends.bill.lastName = "Hey"
friends.bill.number = "098 6789"
friends.bill.address = ['somewhere', 'near', '7th street']

friends.steve.firstName = "Steve"
friends.steve.lastName = "Bye"
friends.steve.number = "364 38989"
friends.steve.address = ['there', 'nearby', '8th street']

friends.julian.firstName = "Julian"
friends.julian.lastName = "Kiss"
friends.julian.number = "33797326 73489"
friends.julian.address = ['here', 'close', '4th street']

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

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

search ("Julian")

this is the console:

{ firstName: 'Julian',
  lastName: 'Kiss',
  number: '33797326 73489',
  address: [ 'here', 'close', '4th street' ] }
{ firstName: 'Steve',
  lastName: 'Bye',
  number: '364 38989',
  address: [ 'there', 'nearby', '8th street' ] }
{ firstName: 'Steve',
  lastName: 'Bye',
  number: '364 38989',
  address: [ 'there', 'nearby', '8th street' ] }
{ firstName: 'Bill',
  lastName: 'Hey',
  number: '098 6789',
  address: [ 'somewhere', 'near', '7th street' ] }
{"firstName":"Julian","lastName":"Kiss","number":"33797326 73489","address":["here","close","4th street"]}

whyyyyy??

It's always printing steve 2x and bill 1x and before and after that what i want... now first julian and in the and julian, if i call bill it shows bill, steve, steve, bill, bill in the same way...

someone there to help me??


#2

You gotta call the list under all your code. So:

Goes at the very bottom.

Then you gotta change all your whatever's and something's to "firstName".

If you just call whatever's, it's not gonna find stuff you want. You gotta call "firstName"