fcc——有关循环的问题

 

 

 

You can’t say that you didn’t find a person until you have searehd through the entire contacts array — which would mean the loop has ended and the firstName has not been found in the array. This: return "No such contact"; should be outside of the loop.

 整个程序

//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 lookUp(firstName, prop){
// Only change code below this line

var result = contacts.filter(function(child){
return child.firstName == firstName;

})[0];

if (!result) return "No such contact";
return result[prop] || "No such property";
// Only change code above this line

}

// Change these values to test your function
lookUp("Akira", "likes");

posted on 2017-06-11 16:58  Willy_Jane  阅读(208)  评论(0)    收藏  举报

导航