Ok, help is needed on 7/8 "Search for a Friend"


#1

Ciao,

I wrote my code as follow:

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
},
yaara: {
firstName: "Yaara",
lastName: "Orlev",
number: "(972) 555-5555",
address: ['Tel-Aviv', 'Israel', 'Jaffa']
},
vitto: {
firstName: "Vittoria",
lastName: "Mariani",
number: "(+39) 555-5555",
address: ['Monza', 'Italy', 'Milan']
}
};

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

When I try to run it I get the Pass. However what I see in the console is the following:

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

Which is "Steve", "Steve" and "Bill" while I have "Steve", "Bill", "Yaara" and "Vittoria".
I spent 30 minutes checking the code and checking on the web for a solution but could not solve it.

Why I get this results and I pass the lesson?

I also tried to put search ("Yaara"); if I do that it puts me "Yaara" at the top and the bottom of the result I display above. Does not make sense at all for me.

Thanks for the help guys, Mattia


#2

There's nothing wrong with the output you're receiving i.e if it's the output you're worried. The test script for the search program is simply looking up Steve twice and Bill only once.


#3

Ok, but why it does not tell me the other 2 that I put?