【问题标题】:JavaScript function and nested arrays questionJavaScript 函数和嵌套数组问题
【发布时间】: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


    【解决方案1】:

    这些返回 false 是因为您将布尔值与字符串值进行比较。

    console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"]) === "I found waldo!");
    console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "shaquon", "julie"]) === "I couldn't find waldo...");
    

    您要进行的比较是:

    console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "waldo", "shaquon", "julie"]) === true);
    console.log(searchForWaldo(["bob", "nancy", "john", "shawnie", "shaquon", "julie"]) === true);
    

    即便如此,您仍需要将字符串记录到控制台,而不是布尔值。

    【讨论】:

    • 谢谢,我遇到了困难。
    【解决方案2】:

    需要做的就写在这里了:

    // - 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..."
    
    function searchForWaldo(arrayOfNames) {
      return containsWaldo(arrayOfNames) ? 'I found waldo!' ? 'I couldn't find waldo...'
    }
    

    此外,这段代码中没有一个嵌套数组。

    【讨论】:

      【解决方案3】:
      // Exercise Six
      const contains = (arr, needle) => arr.includes(needle);    
      
      // Exercise Seven
      const contains = (arr, needle) => arr.includes(needle) ? "I found waldo!" : "I couldn't find waldo...";
      

      Array.prototype.includes()

      【讨论】:

      • 谢谢你的解释。
      猜你喜欢
      • 2019-05-24
      • 2019-09-16
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多