7. Search for a friend


#1

var friends = new Object();
friends.bill = {};
friends.steve = {};
friends.bill.firstName = "Bill";
friends.steve.firstName = "Steve";
friends.bill.lastName = "Idk";
friends.steve.lastName = "Smith";
friends.bill.number = "555 555 555";
friends.steve.number = "666 666 666";
friends.bill.address = ['One Microsoft Way', 'Redmond', 'WA', '98052'];
friends.steve.address = ["One Microsoft Way", 'Redmond', 'WA', '98052'];
var list = function(friends){
for(var firstName in friends) {
console.log(firstName)
}
};
var search = function(name){
for (lulz in friends) {
if(friends[lulz].firstName === name){
console.log(friends[lulz]);
return friends [lulz];
}
}
};
search("Steve");

this is the output i get : { firstName: 'Steve',
lastName: 'Smith',
number: '666 666 666',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Steve',
lastName: 'Smith',
number: '666 666 666',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Steve',
lastName: 'Smith',
number: '666 666 666',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Bill',
lastName: 'Idk',
number: '555 555 555',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Steve","lastName":"Smith","number":"666 666 666","address":["One Microsoft Way","Redmond","WA","98052"]}

Ofcourse something is wrong here, but what?


#2

Try this code instead:

var friends = {};
friends.mohamad = {
firstName: "Mohamad",
lastName: "Bahaa",
address: ['Egypt','Alexandria']
};

var friends = {
bill: {
firstName: 'Bill',
lastName: 'Gates',
number: '01221',
address: ['USA', 'New York']
},
steve: {
firstName: 'Steve',
lastName: 'Jobs',
number: '01001',
address: ['USA', 'London']
}
};

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


#3

The output i get is exactly the same! So if both of our codes are good (because they work) that means codecademy's output is wrong.


#4

Reload the page and try again.


#5

It is giving me the same output as you guys. I think it's a bug


#6

After I refreshed the page it worked.


#7

Good :slight_smile:
.......