美文网首页
Profile Lookup | Free Code Camp

Profile Lookup | Free Code Camp

作者: Marks | 来源:发表于2017-04-29 18:49 被阅读50次

    函数 lookUp 有两个预定义参数:firstName值和prop属性 。
    函数将会检查通讯录是否存在一个联系人的firstName属性等于firstName值,还会检查对应联系人是否存在 prop属性。
    如果它们都存在,函数返回prop属性对应的值。
    如果firstName 值不存在,返回 "No such contact"。
    如果prop 属性不存在,返回 "No such property"。
    "Kristian", "lastName" 应该返回 "Vos"
    "Sherlock", "likes" 应该返回 ["Intriguing Cases", "Violin"]
    "Harry","likes" 应该返回一个数组
    "Bob", "number" 应该返回 "No such contact"
    "Akira", "address" 应该返回 "No such property"

    //错误解法
    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 lookUpProfile(firstName, prop){
    // Only change code below this line
    if(contacts.hasOwnProperty(firstName)){
      if(contacts.hasOwnProperty(prop)){
        return contacts.prop;
      } else {
        return "No such property";
      }
    } else {
      return "No such contact";
    }
    // Only change code above this line
    }
    
    // Change these values to test your function
    lookUpProfile("Akira", "likes");```
    ```
    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){
    // Only change code below this line
    for(var i=0;i<contacts.length;i++){
        if(contacts[i].firstName == firstName){
            if(contacts[i].hasOwnProperty(prop)){
                 return contacts[i][prop];
                 } 
                  return "No such property";       
             }
        } 
         return "No such contact";  //位置是关键!
      
      
    // Only change code above this line
    }
    
    // Change these values to test your function
    lookUp("Akira", "likes");```

    相关文章

      网友评论

          本文标题:Profile Lookup | Free Code Camp

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