Profile Lookup | Free Code Camp

函数 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

你可能感兴趣的:(Profile Lookup | Free Code Camp)