Explaining the Search function - Contact List.7


#1

Hey, so I wrote all of this code, and it doesnt seem to work..

When I run it, the console says "ReferenceError: number is not defined" and I get the message "Oops, did you create a search function?"

I have no idea what the problem is..
I thought it might lay in the friends list, but I cant seem to figure out how.
Any help appreciated.

,,,,,,,,,,,,,,,,,,,,

var friends = {
    bill: {
    firstName: "Bill",
    lastName: "Anderson",
    number: "543456543",
    address: ["Fiolstræde", "34", "1150", "København K"],
    },
    steve: {
    firstName: "Steve",
    lastName: "Johnson",
    number: "634624545",
    address: ["Fiolstræde", "10", "1150", "København K"],
    },
    jack: {
    firstName: "Jack",
    lastName: "Jenkins",
    number: "353460923",
    address: ["Studiestræde", "30", "1150", "København K"],
    },
};


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


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

    }
};
search("Steve")

,,,,,,,,,,,,,,,

Thank you


#2

if you want to use number in your if condition, you should use it in the loop as well:

for(var number in friends) {

now, number will hold the objects (bill, steve, jack) stored in the object friends in turn while it looping over them, and now you can use number in your if condition like you do


#3

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