英文:
Why doesn't my function recognize a value in an array after I update my if statement?
问题
function lookUpProfile(name, prop) {
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property";
}
} else if (contacts[i].firstName[name] === undefined) {
return "No such contact";
}
}
}
英文:
I'm trying to get the data of a contact when the name actually exist in my array and when it doesn't an "No such contact" prompt, and like this it works and I get "Vos"
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 (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property"
}
}
}
}
lookUpProfile("Kristian", "lastName");
But when I add and extra else if it just doesn't recognize the name any more and I get the "No such contact". I guess is something really simple and kind of obvious, but I just don't get why
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 (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property"
}
} else if (contacts[i].firstName[name] === undefined) {
return "No such contact"
}
}
}
lookUpProfile("Kristian", "lastName");
答案1
得分: 0
问题在于在第二段代码中,在第一次迭代后就退出了循环。因此,你只检查了第一个项目,然后返回'没有这样的联系人'。看起来你只想在找不到联系人时才在最后返回。我添加了一个控制台日志,显示了循环经过了多少次迭代,这应该有助于故障排除。
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 (var i = 0; i < contacts.length; i++) {
console.log('循环: ' + i);
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "没有这样的属性";
}
} else if (contacts[i].firstName[name] === undefined) {
return "没有这样的联系人";
}
}
}
console.log(lookUpProfile("Kristian", "lastName"));
英文:
The problem is you are returning from the loop after the first iteration in the second snippet. As such, you are only checking the first item and then returning 'No such contact'. It looks like you only want to return at the very end if not contact has been found. I have added a console log that shows how many iteration the loop goes through, which should help with troubleshooting.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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 (var i = 0; i < contacts.length; i++) {
console.log('Loop: ' + i);
if (contacts[i].firstName === name) {
if (contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else {
return "No such property"
}
} else if (contacts[i].firstName[name] === undefined) {
return "No such contact"
}
}
}
console.log(lookUpProfile("Kristian", "lastName"));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论