这是我的免费代码营挑战“个人资料查询”的代码,我坚持使用这个代码而我的问题是if条件,怎样才能让测试用例进入我的第一个if条件?
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
for ( var i = 0; i < contacts.length; i++) {
if ( contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
else if (firstName !== contacts[i].firstName) {
return "No such contact";
}
else if (contacts[i].hasOwnProperty(prop) === false) {
return "No such property";
}
}
}
lookUpProfile("Harry", "likes");
我应该得到的是“喜欢”的价值,这是[“Hogwarts”,“Magic”,“Hagrid”],为此,它应该进入第一个if条件,并且有问题。
答案 0 :(得分:3)
你的问题是你早早回来了。无论如何,如果满足任何条件,您总是返回contacts
集合的第一次迭代。您需要做的是将答案存储在循环外部的局部变量中,并返回该值。这样做可以确保您在没有过早返回的情况下完成所有迭代。
试试这个:
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) {
var result = undefined;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
result = contacts[i];
}
}
if (result === undefined) {
return "No such contact";
}
if (result.hasOwnProperty(prop) === false) {
return "No such property";
}
return result[prop];
}
console.log(lookUpProfile("Harry", "likes"));
&#13;
更实用和惯用的方法是使用数组方法,例如filter。
答案 1 :(得分:2)
你回来太早,所以当第一个结果与“Harry”不匹配时,它会自动认为找不到联系人。
Seth的答案适用于您的用例,但我认为如果联系人在阵列中的顺序不同,则会因其他用例而出错。如果有更多的联系人在找到初始正确的联系后循环,那么结果的值将在稍后的循环中被覆盖。
我会将lookUpProfile重写为:
function lookUpProfile(firstName, prop) {
var profile = null;
var err = null;
for (var i = 0; i < contacts.length; i++) {
if (firstName !== contacts[i].firstName) {
err = "No such contact";
} else if (contacts[i].hasOwnProperty(prop) === false) {
err = "No such property";
}
if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
profile = contacts[i][prop];
}
}
return (profile) ? profile : err;
}
您也可以查看我创建的JSBin:http://jsbin.com/letesuhope/2/edit?js,console
答案 2 :(得分:0)
也许因为&#34;返回&#34;退出for循环并在第一个项目后停止。尝试使用&#34; console.log&#34;
if ( contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
console.log(i, contacts[i][prop]);
}
else if (firstName !== contacts[i].firstName) {
console.log(i, 'No such contact');
}
else if (contacts[i].hasOwnProperty(prop) === false) {
console.log(i, "No such property");
}
答案 3 :(得分:0)
问题是你在第一个'for'迭代中返回一个值。 您需要使用一个新变量,将搜索分成两步,然后返回结果。
function lookUpProfile(firstName, prop){
var contact;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === firstName) {
contact = contacts[i];
}
}
if (!contact) {
return "No such contact";
}
if (contact.hasOwnProperty(prop) === false) {
return "No such property";
}
return contact[prop];
}