7/8 Can't figure out what I'm doing wrong


#1

Hi,

I am stumped by the "Search for a friend" lesson and I can't figure out what I'm doing wrong.

I think that my list function is OK.

Here is my code:

var friends = {};

friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "514-444-4444",
address: ['One Microsoft Way','Redmond','WA','98052']
};

friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "450-555-5555",
address: ['Two Microsoft Way','Redmond','WA','98052']
};

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

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

So, to find Steve's info, I need to run:
search("Steve");

But it does not work.

Thanks for the help!


#2

What kind of error are you getting? because your code seems almost flawless to me.


#3

The error I receive is "
Oops, try again. It looks like your search function doesn't return contact information for Steve."

In the console I see:

{"firstName":"Steve","lastName":"Jobs","number":"450-555-5555","address":["Two Microsoft Way","Redmond","WA","98052"]}


#4

I found the following code on the forums and it works:

var friends = {};

friends.bill = {
firstName:"Bill",
lastName:"gates",
number:"0171312276",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName:"Steve",
lastName:"jobs",
number:"0171312277",
address:['1 infinit loop', 'California', 'USA']
};
var list = function(myFriends) {
for (var key in myFriends) {
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");

It works. Well, only kinda works.

It returns the following:

{ firstName: 'Steve',
lastName: 'jobs',
number: '0171312277',
address: [ '1 infinit loop', 'California', 'USA' ] }
{ firstName: 'Steve',
lastName: 'jobs',
number: '0171312277',
address: [ '1 infinit loop', 'California', 'USA' ] }
{ firstName: 'Steve',
lastName: 'jobs',
number: '0171312277',
address: [ '1 infinit loop', 'California', 'USA' ] }
{ firstName: 'Bill',
lastName: 'gates',
number: '0171312276',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }
{"firstName":"Steve","lastName":"jobs","number":"0171312277","address":["1 infinit loop","California","USA"]}

But it passed.


#5

I just had the same problem!

In your search function:
if (friends[key].firstname === name) {
"firstname" needs a capital -> firstName