我们有一个联系人列表,里面存储着不同联系人的数组对象。
函数 lookUpProfile 有两个预定义参数:firstName值和prop属性 。
该函数应检查firstName是实际联系人的firstName,给定的属性(prop)是该联系人的属性。
如果它们都存在,函数返回prop属性对应的值。
如果firstName 值不存在,返回 "No such contact"。
如果prop 属性不存在,返回 "No such property"。
以下是相关代码
//Setup
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
var i=0;
for (i=0;i<contacts.length;i++) {
if(contacts[i].firstName==firstName){
if(contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}else{
return "No such property";
}
}
} return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
网友评论