2 回答

TA貢獻1803條經驗 獲得超6個贊
我認為這是讓你感到困惑的 for 循環(huán)。問題要求我們檢查是否name does not correspond to any contacts then return "No such contact"。因此,在您的回答中,您一到達第一個無效聯(lián)系人就
返回,這不是想要的問題。 您應該將自己排除在 for 循環(huán)之外,作為最終的包羅萬象。else if (contacts[i]===name) {return "No such property";}
return "No such contact"
除此之外,當您需要檢查對象中是否存在某個屬性時,您可以使用Object.hasOwnProperty(propertyName). contacts[i]inelse if (contacts[i]===name) {return "No such property";}會給你聯(lián)系人中的實際對象,它永遠不會匹配name.
您將需要獲得name帶括號或點的注釋。例子:
contacts[0] is equal to
{
? ? "firstName": "Akira",
? ? "lastName": "Laine",
? ? "number": "0543236543",
? ? "likes": ["Pizza", "Coding", "Brownie Points"]
}
contacts[0].firstName is equal to "Akira"
contacts[0]["firstName"] is equal to "Akira"
// 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 lookUpProfile(name, prop) {
? ? // Only change code below this line
? ? for (let i = 0; i < contacts.length; i++) {
? ? ? ? const contact = contacts[i];
? ? ? ? if (contact.firstName === name && contact.hasOwnProperty(prop)) {
? ? ? ? ? ? return contact[prop];
? ? ? ? } else if (contact.firstName === name && !contact.hasOwnProperty(prop)) {
? ? ? ? ? ? return "No such property";
? ? ? ? }
? ? }
? ? return "No such contact";
? ? // Only change code above this line
}
lookUpProfile("Akira", "likes");

TA貢獻1869條經驗 獲得超4個贊
先找個人資料,再找房產
function lookUpProfile(name, prop) {
const profile = contacts.find((contact) => contact.firstName === name)
if (!profile) return "No such contact"
const property = profile[prop]
if (!property) return "No such property"
return property
}
// 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 lookUpProfile(name, prop) {
// Only change code below this line
const profile = contacts.find((contact) => contact.firstName === name)
if (!profile) return "No such contact"
const property = profile[prop]
if (!property) return "No such property"
return property
// Only change code above this line
}
console.log(lookUpProfile("Akira", "likes"))
console.log(lookUpProfile("Bkira", "likes"))
console.log(lookUpProfile("Akira", "nikes"))
添加回答
舉報