7. Search for a friend Query


#1


So the code works, but it doesn't return what I was anticipating. Maybe I'm not expecting it correctly but I thought it should just log Steve's entry?

Instead it is giving multiples of certain people's entries... Ie three steve's and one bill but no Mai-lan?

{ firstName: 'Steve',
  lastName: 'Pat',
  number: '2345',
  address: [ '7 Cow Lane', 'Happy Valley', 'SA', '90210' ] }
{ firstName: 'Steve',
  lastName: 'Pat',
  number: '2345',
  address: [ '7 Cow Lane', 'Happy Valley', 'SA', '90210' ] }
{ firstName: 'Steve',
  lastName: 'Pat',
  number: '2345',
  address: [ '7 Cow Lane', 'Happy Valley', 'SA', '90210' ] }
{ firstName: 'Bill',
  lastName: 'Hat',
  number: '1234',
  address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Steve","lastName":"Pat","number":"2345","address":["7 Cow Lane","Happy Valley","SA","90210"]}



var friends = new Object();
friends.bill = {
    firstName: "Bill",
    lastName: "Hat",
    number: "1234",
    address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
};
friends.steve = {
    firstName: "Steve",
    lastName: "Pat",
    number: "2345",
    address: ['7 Cow Lane', 'Happy Valley', 'SA', '90210']
};
friends.mailan = {
    firstName: "Mai-Lan",
    lastName: "Tran",
    number: "3456",
    address: ['1 Mocalta Street', 'Glengowrie', 'SA', '90211']
};
var list = function (friends) {
    for (var key in friends) {
        console.log(key);
    };
};
var search = function (name) {
    for (var key in friends) {
        if (friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        };
    };
};
search("Steve");


#2

codecademy calls some of the function, to test your code is working, this shows in the output, try here, you will see your output is correct


#3

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