1
我在freecodecamp.com並停留在配置文件查詢的問題。我在這裏失蹤了什麼?任何人都可以指出我嗎?如果我註釋掉這個條件firstName == contacts[i].firstName
它返回4次沒有這樣的聯繫 & 1次沒有這樣的屬性我做錯了什麼?Javascript object proccesing confusion
使用案例:
- "Kristian", "lastName" should return "Vos"
- "Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
- "Harry","likes" should return an array
- "Bob", "number" should return "No such contact" //**fails**
- "Akira","address" should return "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
for(var i = 0; i< contacts.length;i++){
if(firstName == contacts[i].firstName &&
contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
/*if(firstName == contacts[i].firstName){
return "No such contact" ;
}*/
if(contacts[i].hasOwnProperty(prop) === false){
return "No such property";
}
}
}
//test strings
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Harry", "likes"));
console.log(lookUpProfile("Bob", "number"));
console.log(lookUpProfile("Akira", "address"));
只是 – Coder