【发布时间】:2021-04-10 01:51:00
【问题描述】:
第一次测试我得到“假”,第二次测试我得到“真”。我想创建一个将评估这些值的函数。我以为我在函数中正确执行了我的代码,但我没有得到我想得到的东西。我不明白我在这里做错了什么。任何帮助将不胜感激。谢谢。
// - Given the arrayOfNames, determine if the array contains the given name.
// - If the array contains the name, return true. If it does not, return false.
// - Hint: Use a loop to "iterate" through the array, checking if each name matches the name parameter.
//
// Write your code here ????
function contains(arrayOfNames, name) {
for (index = 0; index < arrayOfNames.length; index++) {
if (arrayOfNames[index] === name) {
return true;
} else {
return false;
}
}
}
// -------TESTS---------------------------------------------------------------
// Run these commands to make sure you did it right. They should all be true.
console.log("-----Tests for Exercise Five-----");
console.log("* Returns true when the array contains the name.");
console.log(contains(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"], "nancy") === true);
console.log("* Returns false when the name is not in the array");
console.log(contains(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"], "fred") === false);
【问题讨论】:
标签: javascript arrays function