7. search for a friend


#1



It doesn't return the information for Steve. But that's not even my problem, I don't understand the entire last paragraph, why do i need the friends[name].firstName and why do i need console.log and return


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "1234567890",
        address: ['Jones Street', 'Oakville', 'L6L 5W5', 'Canada']
        },
    steve:{
        firstName: "Steve",
        lastName: "Jobs",
        number: "0987654321",
        address: ['Queen Street', 'Rosewood', 'Australia']
    }
};
var list = function(friends) {
    for(var firstName in friends) {
        console.log(firstName);
    }
}
var search = function(name) {
    for(var name in friends) {
        if (friends[name].firstName === name) {
            console.log(friends[name]);
            return friends[name];
        } else {
            return "No match!";
        }
    }
}


#2

Update: I kind of understand it now but it still doesn't return Steve


#3

what does kind of understand mean?

a function ends the moment a return keyword is reached, so return no match should be outside the for loop, otherwise no match is returned in the first iteration of the for loop


#4

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