我正在通过freecodecamp工作,并参与以下练习:
现在我的代码如下:
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
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name) {
foundName += 1;
}
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
return "No such property";
}
}
if (foundName < 1) {
return "No such contact"
};
}
var foundName = 0;
// Only change code above this line
// Change these values to test your function
var ans = lookUpProfile("Bob", "number");
console.log(ans);
&#13;
所以我用我的for循环遍历预定义的数组,并且我检查了name == firstName和对象具有prop属性的实例。在这些情况下,我将归还财产。否则,我返回&#34;没有这样的财产&#34;。我还改变了我的变量foundName,这样当firstName与循环中的name匹配时,foundName得到一个正值。如果foundName小于1(即找不到名称匹配),那么我返回&#39;没有这样的联系&#39;。
现在,当我在浏览器中运行它并查看控制台时,它似乎完美无缺。但是,当我将此答案输入freecodecamp时,我得到:
&#34; Bob&#34;,&#34; number&#34;应该返回&#34;没有这样的联系&#34; &#34; Bob&#34;,&#34;马铃薯&#34;应该返回&#34;没有这样的联系&#34;
但是,如果我把,例如,&#34; Bob&#34;和&#34;数字&#34;进入这个功能,我确实得到了#34;没有这样的联系&#34; ......我一定会错过这里显而易见的东西,但我对此非常困惑!!
答案 0 :(得分:1)
问题在于您的变量 foundName 每次调用函数 lookUpProfile 时更改全局变量 foundName 所以当您调用函数时** lookUpProfile ** with&#39; bob&#39;作为参数,它将返回 undefined ,因为 foundName 的值大于1,因此您需要将变量范围限定为功能块范围,您应该定义变量 foundName 不在其中,也停止使用关键字 var ,而是使用让
查看下面的解决方案
//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
// Only change code below this line
let foundName = 0;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name) {
foundName += 1;
}
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
return "No such property";
}
}
if (foundName < 1) {
return "No such contact"
};
}
// Only change code above this line
// Change these values to test your function
// Change these values to test your function
lookUpProfile("Akira", "likes");
&#13;
答案 1 :(得分:0)
您的对象声明不正确:contacts
是一个数组。
该功能有效,但您需要将foundName
保留在函数中而不是外部。否则,如果你运行你的功能两次,它将会中断。
以下是工作代码:
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) {
let foundName = 0;
for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name) {
foundName += 1;
}
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
} else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop) == false) {
return "No such property";
}
}
if (foundName < 1) {
return "No such contact"
};
}
console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Bob", "number"))
&#13;