7. Search for a Friend


#1

I keep getting the error message "It looks like your search function doesn't return contact information for Steve" and can't seem to find my error. Any help would be appreciated!

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
friends.kelsey = new Object();
friends.alec = new Object();
friends.matt = new Object();

var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
}
};
var friends = {
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-4444",
address: ['One Apple Way', 'Hollywood', 'CA', '90210']
}
};

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

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


#2

@pens7187,

+++ 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

I followed that link, and it appears that my code is very similar, yet I still get the error saying it doesn't return contact information for Steve

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
friends.kelsey = new Object();
friends.alec = new Object();
friends.matt = new Object();

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
}
};
var friends = {
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-4444",
address: ['One Apple Way', 'Hollywood', 'CA', '90210']
}
};
var friends = {
Kelsey: {
firstName: "Kelsey",
lastName: "Sozanski",
number: "(412) 123-4567",
address: ['987 Sesame St', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Alec: {
firstName: "Alec",
lastName: "Evachek",
number: "(412) 555-5555",
address: ['152 Riverview Ave', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Matt: {
firstName: "Matt",
lastName: "Nelson",
number: "(412) 555-4444",
address: ['123 North Side Ave', 'Pittsburgh', 'PA', '15213']
}
};

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];
}
}
};

list(friends);
search('Steve');


#4

@pens7187,
BEFORE your list function definition
add the code-line

console.log( friends );

and look at how many friends you have.....


#5

It prints out the last information for Matt only, and then still get the error message saying it can't return contact information for Steve

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();
friends.kelsey = new Object();
friends.alec = new Object();
friends.matt = new Object();

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
}
};
var friends = {
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-4444",
address: ['One Apple Way', 'Hollywood', 'CA', '90210']
}
};
var friends = {
Kelsey: {
firstName: "Kelsey",
lastName: "Sozanski",
number: "(412) 123-4567",
address: ['987 Sesame St', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Alec: {
firstName: "Alec",
lastName: "Evachek",
number: "(412) 555-5555",
address: ['152 Riverview Ave', 'Pittsburgh', 'PA', '15202']
}
};
var friends = {
Matt: {
firstName: "Matt",
lastName: "Nelson",
number: "(412) 555-4444",
address: ['123 North Side Ave', 'Pittsburgh', 'PA', '15213']
}
};

console.log(friends);

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];
}
}
};
list(friends);
search('Steve');