Did you create a function called search?


#1


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


I don't understand I have a search function.

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


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Long",
        number: "513-555-1234",
        address: ['Street', 'Cincinnati', 'OH', '45225']
    },
    steve: {
        firstName: "Steve",
        lastName: "Huo",
        number: "513-555-3258",
        address: ['Avenue', 'Cincinnati', 'OH', '45225']
    }
};

var search = function(){

    for (var firstName in friends) {
    if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        } else {
            console.log("No match found");
            }
        }
};

search = prompt("Enter friend's name");


#2

You didn't created a list function!
refer to exercise > List 'em all!


#3

i tried this but it still says the error about missing function called search....

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Long",
        number: "513-555-1234",
        address: ['Street', 'Cincinnati', 'OH', '45225']
    },
    steve: {
        firstName: "Steve",
        lastName: "Huo",
        number: "513-555-3258",
        address: ['Avenue', 'Cincinnati', 'OH', '45225']
    }
};

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

var search = function(){

    for (var key in friends) {
    if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        } else {
            console.log("No match found");
            }
        }
};

list(friends);
search = prompt("Enter friend's name");

#4

I tested this code and made some changes , let me point out the change.

hint**
search = prompt("Enter friend's name");

As when you call search it needs name variable but you're passing search variable.

Also its not a good idea to define function (search() function) and a variable with same name (i.e is search)

modifications:
You don't need this.. list(friends);


#5

What do you mean? isn't search = my way of calling the search function? at least that is what I was trying to do, i wanted to enter a prompt and have the search function fire off...how should I do that then?


#6

You have included many redundant things inside your code.
Even you delete last two lines of you code, It will work!

list(friends);
search = prompt("Enter friend's name");

@dragon81
update**
but you need to pass name argument inside your search function!

Codecademy tests condition and you do not need to write extra code to test it!
I hope it helps :slight_smile:


#7

but how would I do it with a prompt? I got it to work now but I dont' understand why I needed the list and why I couldn't pass a prompt value into the search function


#8

Read the instruction ,do they asked to introduce prompt?

You need to pass this function an argument: name

Also delete last two lines of code. (delete the prompt,its not needed)


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.