7. search for a friend


#1

the error comes out as
Oops, try again. Did you create a function called search?

and this is on the screen
Bill
Steve
john
calvin
ReferenceError: key is not defined

heres the code:

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
},
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 335-5555",
address: ['werwery','Redmond','WA','98052']
},
john: {
firstName: "John",
lastName: "Doe",
number: "(123) 123 1234",
address: ['One Microsoft Way','werwer','WA','98052']
},
calvin: {
firstName: "Calvin",
lastName: "Hobbs",
number: "(789) 789 7896",
address: ['One Microsoft Way','Redmond','werwer','98052']
}
};

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

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

whats wrong!?


#2

@terablaster18572,
Lowercase Steve and Bill like calvin and john.

+++ friends list and search and for-in explained.
Look here for an explanation
object
function
for-in-loop
list-function
search-function
http://www.codecademy.com/forum_questions/545275fd8c1ccc8e3e000a23#comment-5457b12c9c4e9d87cb00003f


#3

alrite so i changed the
var search = function (friends) {
into
var search = function (name) {

and tried search("steve") again (lowercased the letters as you said)

and got this as an output

bill
steve
john
calvin
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(206) 335-5555',
address: [ 'werwery', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(206) 335-5555',
address: [ 'werwery', 'Redmond', 'WA', '98052' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '(206) 555-5555',
address: [ 'One Microsoft Way', 'Redmond', 'WA', '98052' ] }

but it doubles the output.
i think it might be because of the console and return line
but why does it return that way?