Search for a friend


#1


My code prints out both Bills and Steves contact information even though I'm only calling Steve ): It worked until the search function so I'm guessing that's the problem, but I can't find it!


var friends = new Object();

friends.bill = {
    firstName: "Bill",
    lastName: "Billson",
    number: "12345",
    address: ['microsoft','USA','11333']
};

friends.steve = {
    firstName: "Steve",
    lastName: "Steveson",
    number: "6789",
    address: ['apple','USA','11444']
};

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

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

list(friends);

search("steve");


#2

bill and steve are both printed because you call your list function:

list(friends);

which list all your contact, search doesn't yield any result. search("steve"); won't be found, string comparison is case sensitive (steve vs Steve)

also, the exercise might output more because of exercise validation.

To see what your code truly outputs, you can run your code here


#3

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