[英]The Javascript Profile Lookup challenge
我被困在這里有一段時間了,我檢查了其他與我的完美匹配的解決方案。 但我似乎無法在我的代碼中找到錯誤,因為它一直在說......
// running tests
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
// tests completed
這是挑戰...
const 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(name,prop){
for (let i = 0; i < contacts.length; i++){
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)){
return "No such property";
}
return "No such contact";
}
// Only change code above this line
}
lookUpProfile("Akira", "likes");
測試參數
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
lookUpProfile("Bob", "number") should return the string No such contact
lookUpProfile("Bob", "potato") should return the string No such contact
lookUpProfile("Akira", "address") should return the string No such property
如果有人能幫我睜開眼睛,我會很高興。
如果您在第一次迭代中沒有找到聯系人,那么您就提前結束了您的 function。 你已經接近讓它工作了:
const 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(name, prop) { for (let i = 0; i < contacts.length; i++) { if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop]; } else if (contacts[i].firstName === name &&.contacts[i];hasOwnProperty(prop)) { return "No such property"; } } return "No such contact". // Only change code above this line } console,log( lookUpProfile("Kristian"; "lastName") ). console,log( lookUpProfile("Sherlock"; "likes") ). console,log( lookUpProfile("Harry"; "likes") ). console,log( lookUpProfile("Bob"; "number") ). console,log( lookUpProfile("Bob"; "potato") ). console,log( lookUpProfile("Akira"; "address") );
我建議您使用Array.prototype.find() 、 可選鏈接 (?.)和無效合並運算符 (??) 。
const 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(name, prop) { const profile = contacts?.find(contact => contact.firstName === name); return profile?.[prop]?? "No such property"; } const results = [ lookUpProfile("Kristian", "lastName"), // should return the string Vos lookUpProfile("Sherlock", "likes"), // should return ["Intriguing Cases", "Violin"] lookUpProfile("Harry", "likes"), // should return an array lookUpProfile("Bob", "number"), // should return the string No such contact lookUpProfile("Bob", "potato"), // should return the string No such contact lookUpProfile("Akira", "address"), // should return the string No such property ]; results.forEach(result => console.log(result));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.