gpt4 book ai didi

javascript - freecodecamp 不接受 JS 代码

转载 作者:行者123 更新时间:2023-12-03 01:45:56 24 4
gpt4 key购买 nike

我正在使用 freecodecamp 并进行以下练习:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

现在我的代码如下:

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);

因此,我使用 for 循环遍历预定义的数组,并检查 name == firstName 以及对象具有 prop 属性的实例。在这些情况下,我将归还属性(property)。否则,我返回“No such Property”。我还更改了变量foundName,以便当firstName 与循环中的名称匹配时,foundName 获得正值。如果foundName小于1(即没有找到匹配的名字),那么我返回“没有这样的联系人”。

现在,当我在浏览器中运行它并查看控制台时,它似乎工作得很好。然而,当我在 freecodecamp 中输入这个答案时,我得到:

“Bob”,“number”应返回“No such contact”“Bob”、“potato”应该返回“No such contact”

但是,例如,如果我将“Bob”和“number”放入函数中,我确实会得到“没有这样的联系”...我一定在这里遗漏了一些明显的东西,但我对此感到非常困惑!

最佳答案

问题出在变量foundName中,每次调用函数lookUpProfile时,都会更改全局变量foundName,因此当您调用该函数时** LookUpProfile** 以 'bob' 作为参数,它将返回 undefined 因为 foundName 的值大于 1 ,因此您需要将变量的范围限定为功能 block 作用域,您应该在函数内部而不是外部定义变量 foundName ,也停止使用关键字 var ,而是使用 let

看下面的解决方案

//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");

关于javascript - freecodecamp 不接受 JS 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50642585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com