我嘗試通過配置文件查找來完成任務。它應該搜索道具並返回他的價值,如果它不能,那麼從條件 的「沒有這樣的接觸」或「沒有這樣的屬性」依賴我不明白爲什麼最後一個條件(else語句)不執行。這是代碼。我被困在循環任務中
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 i = 0;
while (contacts[i]) {
if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
i++;
}
if (!contacts.firstName) {
return "No such contact";
}
else {
return "No such property";
}
}
lookUpProfile("Akira", "address");`
它返回「沒有這樣的聯繫」,雖然應該返回「沒有這樣的屬性」。
'接觸' 是一個數組,也沒有 '的firstName'! – MHS