【发布时间】:2021-04-10 13:26:35
【问题描述】:
我需要一些帮助,你看我必须在练习 6 中确定,如果数组包含名称“waldo”并且我的函数是否通过测试,它确实通过了,并且控制台对两个测试都记录为 true。现在在练习 7 中,它希望我再次执行此操作,确定数组是否包含“waldo”以及它是否包含我的函数是否应该通过这些测试。如果我尝试了这么多不同的场景并且我不断让控制台为两个测试记录 false 或者给我一个错误消息“arrayOfNames”未定义,我的问题。我正在尝试使用提示,只是调用我的 containsWaldo 函数没有成功。关于我在这里做错了什么的任何想法,我都复制了两个练习的问题,以使其更清楚。提前谢谢你。
----------------------------
console.log("Exercise Six");
// - Given the arrayOfNames, determine if the array contains the name "waldo".
// - The name waldo will be all lower-case.
// - If the array contains "waldo", return true. If it does not, return false.
// - Hint: You don't have to write another loop, or copy-paste your previous function.
// Just call your previous function, "contains," with the array and the name "waldo" and return the result.
//
// Write your code here ????
function containsWaldo(arrayOfNames) {
return contains(arrayOfNames, "waldo");
}
// -------TESTS---------------------------------------------------------------
// Run these commands to make sure you did it right. They should all be true.
console.log("-----Tests for Exercise Six-----");
console.log("* Returns true when waldo is in an array");
console.log(containsWaldo(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"]) === true);
console.log("* Returns false when waldo is not in the array");
console.log(containsWaldo(["bob", "nancy", "john", "shawnie", "shaquon", "julie"]) === false);
// ----------------------------------------------------------------------------------------------
console.log("Exercise Seven");
// - Given the arrayOfNames, if the array contains waldo, then return "I found waldo!"
// If the array does not contain waldo, then return "I couldn't find waldo..."
// - Hint: Don't actually search for waldo! Just call your other function, "containsWaldo".
//
// Write your code here ????
function searchForWaldo(arrayOfNames) {
return containsWaldo(arrayOfNames);
}
// my code here, will log false for both test cases
// -------TESTS---------------------------------------------------------------
// Run these commands to make sure you did it right. They should all be true.
console.log("-----Tests for Exercise Seven-----");
console.log("* Returns 'I found waldo!' when waldo is in an array");
console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"]) === "I found waldo!");
console.log("* Returns 'I couldn't find waldo...' when waldo is not in the array");
console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "shaquon", "julie"]) === "I couldn't find waldo...");
【问题讨论】:
标签: javascript arrays function