我正在尝试返回“史蒂夫”的联系信息,但是代码学院告诉我它没有正确返回。


  您的搜索功能似乎未返回Steve的联系信息。


我在此代码中找不到任何错误。您可以帮助我在搜索功能中查找任何语法或逻辑错误吗?

var friends = {
    bill: {
        firstName: "bill",
        lastName: "gates",
        number :21415,
        address: ["mcklsn", "wcnskln"]
    },
    Steve: {
        firstName: "bill",
        lastName: "gates",
        number: 21415,
        address: ["mcklsn", "wcnskln"]
    }
};

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

    for (steve in friends)
        console.log(steve);
};

var search = function(name)
{
    for (var i in friends)
    {
        if (friends[i].firstName === name)
        {
            console.log(friends[i]);
            return friends[i];
        }
        else
            console.log("contact doesn't exist!");
    }
};

最佳答案

您应该真正注意所写的​​内容,不要盲目地按Ctrl-C Ctrl-V。您不是用正确的名字称呼您的friends,如果有人对我说的话,我会被冒犯。哎呀,如果你叫他比尔,史蒂夫·乔布斯会大怒!

这应该工作:

var friends = {
    Bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: 21415,
        address: ["mcklsn", "wcnskln"]
    },
    Steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: 21416,
        address: ["mcklsnn", "wcnsklnn"]
    }
};

search("Bill");
search("Steve");

关于javascript - javascript中的搜索功能错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34858103/

10-12 22:27