Stuck on Contact List Search


#1

Well, I admit it, sometimes can't see the forest......

I'm having an issue with the following code. Keeps asking me if I've created a function called Search. Followed the examples but sometimes have trouble distinguishing placeholder text. Thanks.
var friends = {

bill: {

firstName: "Bill",
lastName: "Gates",
number: "(206)-555-5555",
address: ['One Microsoft Way','Redmond', 'Washington','12345']

},

steve: {

firstName: "Steve",
lastName: "Jobs",
number: "(802)-555-5555",
address: ['One Heavenly Way','Cupertino', 'California','67899']

}

};
var list = function(friends){

for (var firstName in friends){
    console.log(firstName)
}

};

var search = function(name){
for (var key in friends)

if(friend[key].firstName === 'Steve'){
    console.log(friends[firstName, lastName, number, address]);
    return friends[firstName, lastName, number, address];
}
else {
    console.log("That name is not listed.")
}

};
search()


#2

You're supposed to do it like this, actually:

var friends = {}

So I'd say you should take out that other bracket, too.

I may not be correct with these corrections, for you have done it in a different way than I have. But try the corrections. :smiley:

Before these:

You need to put the "friend" on it to label it:

friends.bill:{

friends.steve:{

In here:

You have to use "key" instead of "firstName" because we are not only looking at the first name of the friends, but the keys. Then, I print the key out.

You have to replace 'Steve' with name.

And... you did it kind of complicated after that. In this part:

A MUCH simple way is that you can use "key" to print them all out! Like this:

console.log(friends[key]);
return(friends[key]);

Also, another bracket after that search function.

When searching, too, put the first name of each person you wrote.

search("_______");

Hope it helped!:smiley:

P.S. Key is the properties of the people you listed.


#3

​Yo Ragezapper.,

Thanks​ for the reply. I will say this, I'm getting a different error message anyway. Always a sign of progress.

If I may let's take the code in sections because I was not too sure about your first suggestions.

How does this section look?

var friends = {

bill: {

firstName: "Bill",
lastName: "Gates",
number: "(206)-555-5555",
address: ['One Microsoft Way','Redmond', 'Washington','12345']

},

steve: {

firstName: "Steve",
lastName: "Jobs",
number: "(802)-555-5555",
address: ['One Heavenly Way','Cupertino', 'California','67899']

}

};

I have created an object called friends. The object has two elements, Bill and Steve separated by a comma and enclosed in their own curly braces and then an ending curly brace to complete the one after var friends = {

I also tried friends.bill but I get an error. It doesn't like the period because it's expecting a colon.

So can you tell me if this part is right.

This second chunk was from a previous lesson. It asks to print out the firsName in each of the entries in the object. Maybe I should take this out altogether.

var list = function(friends){

for (var firstName in friends){
    console.log(firstName)
}

};

Finally this chunk. The error is something to the effect that it's expecting a curly brace after an object literal. I think you had me on the right track looking for errant curly braces but I'm still not quite there.

var search = function(friends){
for (var key in friends)

if(friend[key].firstName === 'name'){
    console.log(friends[firstName, lastName, number, address]);
    return friends[firstName, lastName, number, address];
}
else {
    console.log("That name is not listed.")
}

};

search('steve')

​It's all much appreciated.

All the best,

Mark​


#4

I used:

var search = function(name){
for (var key in friends)

if(friends[key].firstName === name)

{
    console.log(friends[key]);
    return friends[key];
}

else
{
console.log("That name is not listed.");
}

};

This has allowed me to actually log to the console the correct name if found but still has unintended consequences in that I am not able to ONLY list that specific searched friends information. It will list my searched friends information and then proceed to list everyone else's as well afterward.

Keep in mind that in your code you have some typos/errors:

1.you have "friend[key]" instead of "friends[key]" (note the plural).

  1. I believe that listing out the specific keys is unnecessary as I was not able to list anything with firstName, lastName, etc listed out as you have.

I am extremely novice at coding so please don't take any of this as a final word on the subject.


#5

So I ended up with this:
var friends = {

bill: {

firstName: "Bill",
lastName: "Gates",
number: "(206)-555-5555",
address: ['One Microsoft Way','Redmond', 'Washington','12345']

},

steve: {

firstName: "Steve",
lastName: "Jobs",
number: "(802)-555-5555",
address: ['One Heavenly Way','Cupertino', 'California','67899']

}

};

var search = function(name){
for (var key in friends)
if(friends[key].firstName === name){
console.log(friends[key]);
return friends[key];
}
else {
console.log("That name is not listed.")
}

};
search("Steve")

Codeacdemy gave me a pass to the next lesson but it really doesn't work right. It returns all the possible answers. If anyone wants to help me figure out why I'd be grateful.

Thanks.

Mark


#6

Mine works the same way :smiley: Hehe maybe it's ok?


#7

I also got the 'Oops, try again.Did you create a function called search?' error message with a for loop that used firstName instead of the word key and was passed to the next lesson when I changed firstName to key. The function returns all information for both contacts instead of the matching contact. I do not understand why.


#8

Ok, so apparently the window that shows results has other instructions it follows besides yours.

If you add a line of nonsense to be logged to the console after you call your search function to look for a particular contact, you will (if I understand correctly what's happening) see the actual results of your code. It will be delineated by the nonsense line from the rest of what the window is doing independent of you.

I believe these other instructions are to show you all returned values, which are normally not logged to the console, and to show you the contents of all the objects you have available to manipulate. My window thus shows me the requested info from calling search, my nonsense line, the returned contact info from calling search, and then each object including the one that was initially displayed.

You pass because your code works, the rest is the window responding to its other instructions. It is important to note that I could be completely wrong about the nature of said instructions. I would appreciate clarification if this is the case.