8. Victory! Why can't I call the search function?


#1

Why can't I call the search function? I've tried it with the object property Steve, but I get the error message: ReferenceError: steve is not defined.


var friends = {
    bill: {
        firstName: "First Name: Bill",
        lastName: "Last Name: Gates",
        number: "Number: 7757508821",
        address: ["Address: 8350 Cub Court", "Reno", "NV", 89506]
        },
    steve: {
        firstName: "Steve",
        lastName: "Last name: Jobs",
        number: "Number: 7759723290",
        address: ["Address: 9670 Oakley Ln", "Reno", "NV", 89521]
        }
};

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

var search = function(name) {
    for (var prop in friends) {
        if (friends[prop].firstName === name) {
            console.log(friends[prop]);
            return friends[prop];
        } else {
            console.log("Friend not found.");
        }
    }
};

search(steve);


#2

Because steve is not defined. search() function takes 1 parameter name which should be string variable because friends[prop].firstName returns string value. So, should be:

search("Steve");

Also friends object should be:

var friends = {
    steve:  {
        firstName: "Steve",
        lastName: "Ben",
        number:  "(206) 555-5555",   
        address: ['One Microsoft Way','Redmond','WA','98052']
    },
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number:  "(206) 533-5555",
        address: ['One Microsoft Way','Redmond','WA','98055']
    }  
};

In your case firstName: "First Name: Bill". It's mean that firstName === "First Name: Bill" but NOT "Bill".


#3

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