(Lesson 12 === Search for a Friend) {Function not returning contact info of steve; return false;}


#1

Hi, I've tried for a long time to get this working. I've gone over it about 20 times now, and can't see my mistake. I'd appreciate any help that can steer me in the right path.

//My Code:

var friends = new Object ();

friends.bill = {
firstName: "Billy",
lastName: "Cranston",
number: "0800-Blue-Ranger",
address: ["3 Ranger Drive, Arcadia Bay, Someplace, 02012"],
};

friends.steve = {
firstName: "Steven",
lastName: "Fox",
number: "0800-Tekken-Scams",
address: ["Mishima Labs Cryo-Capsule 4, Nina's Belly"],
};

var list = function (obj) {
for (jack in obj) {
console.log(jack);
};
};

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

list(friends);
//search("steve");

// I GET AN EMPTY RESPONCE, WITH THE CA PROMPT SAYING "It looks like your search function doesn't return contact information for Steve."


#2

For starters, this variable name doesn't convey any meaning relating to its purpose. It's a key iterator, so why not just call it key.

for (var key in obj) { ...

We are searching inside the data of the individual friends objects, not the key names, themselves. Remember that JavaScript is case sensitive. "steve" and "Steve" are two different string values.

search("Steve");

Now it will find the firstName that corresponds.


#3

These explanations are the best I've found yet.
The way they are worded, as weird as it may sound, actually made what the lesson was trying to teach click for me. I have been working on this part myself for way too long it feels like, and it was to no avail. Continuously checking my post, and the posts of others, made me realize that this particular section is very weak in terms of explanation, education, and understanding which is quite unfortunate. HOWEVER, due to the devoted "teachers" here in the forums, clouds can be swept aside and some understanding can be formed.

I wish there was another problem for me to try, now, to ensure that I actually am understanding it correctly the way I am understanding it...

Ps I know this is not my post, but like I said, this is the post with the most helpful answers I've found in the sea of posts about this section.


#4

Oh wow, thanks, it's working now.
I can't believe I didn't realise that. I was pretty certain the error was within the function.

Also, thanks for the advice on the jack/key variable name thing. You've made a highly valid point. Strangely it wasn't until you mentioned it that it clicked in my head why to use "key" and not just a random thing. I've just been defaulting to "jack" to name random things.