Search a list


#1


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

i can't find where i did wrong in my code, can anyone help me figure it out?

var friends = {
    bill: {firstName: "Bill",
    lastName: "Allen",
    number: "0910-453-746",
    address: ["China", "Guangzhou", "Penyu District"]},
    
    steve: {firstName: "Steve",
    lastName: "Kao",
    number: "0909-412-444",
    address: ["Taiwan", "Taipei City", "Chungzhang District"]}
};

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

var search = function(name) {
    for (var key in friends ) {
        if (friends[key].firstname === name) {
            console.log(friends[key]);
            return friends[key];
        } else { return "invalid" 
        }
    }
};

search("steve");


#2

if (friends[key].firstname === name) {

It should be friends[key].firstName

also you need to remove the else statement.A function ends at that moment whenever a return keyword is reached.


#3

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