7. Search for a friend still


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#

Oops, try again. Did you create a function called search?

I've been working on this lesson for most of the day and everything I did looks and seems right. I thought I declared a function called search and I don't see any other way to do it. I've looked over other posts and they look like mine. Can someone PLEASE look at my code and tell me what I'm missing or what I need to fix to make it right? Thank you in advance.


var friends = {
	bill: {
		firstName: "Bill",
		lastName: "Gates",
		number: "206 555-5555",
		address: ["One Microsoft Way", "Redmond", "WA", "98052"],
	},

	steve: {
		firstName: "Steve",
		lastName: "Martin",
		number: "302 555-5445",
		address: ["725 Vandever Avenue", "Los Angeles", "CA", "10276"],
	},

	helen: {
		firstName: "Helen",
		lastName: "Featherer",
		number: "813 347-3523",
		address: ["13906 Henson Circle", "Tampa", "FL", "33625-"]
	}
};
var list = function(friends) {
	for (var x in friends) {
		console.log(x);
	}
var search = funtion(friends); {
		for (var key in friends) {
			if (friends[key].firstName === name) {
				console.log(friends[key]);
			} else
				return "none";
		  }
	}
	list(friends);
	search("Helen");
};


#2

How does x come in? You are meant to use the key to search in friends right? You're also missing curl brace to close that function.

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

For the above, you should be giving the function a parameter/argument of name which you would like to call later. This is stated explicitly in the instructions.

    list(friends);

    var search = function (name) {
        for (var key in friends)
        {
            if (friends[key].firstName === name) {
                console.log(friends[key]);
                return friends[key];
            } //Not bad to add an else statement here.
        }
    };
    search("firstName"); //You should be using the key here I presume.

I really hope I helped! :slight_smile:


#3

I appreciate your help but now it says SyntaxError: Illegal return statement. What does that mean?


#4

Err.. Okay, let me put it all together and then see:

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

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

#5

You added commas at the end for bills and steves addresses; however, I don't think you need to do that.

I'm not quite sure how for in loops work, but I also don't see where the x comes in to play in this exercise.

[quote="mrs2nsmith,

You added a smelly colon (pun intended :slight_smile: ) right after your parentheses.

You might want to open the else statement with a curly bracket :slight_smile:

Could I have a link to the exercise to better enlighten me about what else might be wrong with your code?


#6

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661


#7

It is not required to use key as your variable name, using x is fine. Often you will see it done like this:

    for (var friend in friends) {

That makes it clear that you are looping though each friend in the friends array.


#8

Oops! Thanks for the insight.

I guess I had my understanding of it in 1D.

Thank you.