美文网首页
FCC 题目 Profile Lookup

FCC 题目 Profile Lookup

作者: AaronSheen | 来源:发表于2017-12-31 22:09 被阅读0次
    //初始化变量
    var contacts = [
        {
            "firstName": "Akira",
            "lastName": "Laine",
            "number": "0543236543",
            "likes": ["Pizza", "Coding", "Brownie Points"]
        },
        {
            "firstName": "Harry",
            "lastName": "Potter",
            "number": "0994372684",
            "likes": ["Hogwarts", "Magic", "Hagrid"]
        },
        {
            "firstName": "Sherlock",
            "lastName": "Holmes",
            "number": "0487345643",
            "likes": ["Intriguing Cases", "Violin"]
        },
        {
            "firstName": "Kristian",
            "lastName": "Vos",
            "number": "unknown",
            "likes": ["Javascript", "Gaming", "Foxes"]
        }
    ];
    
    
    function lookUp(firstName, prop){
    // 请把你的代码写在这条注释以下
      for (var i = 0; i < 4; i++) {
        if (contacts[i].firstName == firstName) {
             if (contacts[i].hasOwnProperty(prop) === true) {
               return contacts[i][prop];
             } else {
               return "No such property";
             }
          } 
      }
      return "No such contact";   //这句应该在for循环之外
    // 请把你的代码写在这条注释以上
    }
    // 你可以修改这一行来测试你的代码
    lookUp("Sherlock", "likes");
    

    return "No such contact" 一开始在for循环之内,导致无法遍历整个数组,因为遇到return就会退出函数,因此应该放在for循环之外。

    相关文章

      网友评论

          本文标题:FCC 题目 Profile Lookup

          本文链接:https://www.haomeiwen.com/subject/cbxagxtx.html