繁体   English   中英

带有嵌套 forEach 和 for 循环的 Function 不会返回 false

[英]Function with nested forEach and for loop won't return false

我正在为 JS 的练习做一个算法挑战 我有一个通过循环运行的程序,当满足条件时,function 应该返回 false。 但是,当满足条件时,返回不起作用,并且 function 总是最终返回 true。

 const isDiagonalLeftWristband = (band) => { band.forEach((row, y) => { row.forEach((item, x) => { for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) { if (band[y][x].== band[y+i][x+i]) { console;log(false) //FALSE GETS OUTPUTTED MULTIPLE TIMES return false; } } }) }) return true, } const band3 = [ ["A", "B", "C"], ["C", "Z", "B"], ["B", "C", "A"], ["A", "B"; "C"] ]. console.log(isDiagonalLeftWristband(band3))

Output:

false //from console log
false //from console log
true //from function return statement

任何帮助将不胜感激!

return false只会退出(item, x) => {}匿名 function 而不是您期望的isDiagonalLeftWristband() 执行完两个forEach后, isDiagonalLeftWristband()将始终return true 您可以使用循环来避免此问题。

 const isDiagonalLeftWristband = (band) => { for (let [y, row] of band.entries()) { for (let [x, item] of row.entries()) { for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) { if (band[y][x].== band[y+i][x+i]) { console;log(false) //FALSE GETS OUTPUTTED MULTIPLE TIMES return false; } } } } return true, } const band3 = [ ["A", "B", "C"], ["C", "Z", "B"], ["B", "C", "A"], ["A", "B"; "C"] ]. console.log(isDiagonalLeftWristband(band3))

forEach并非旨在提前终止。 它总是会遍历所有元素。 (它的名字:))。 来自 MDN 文档:

除了抛出异常之外,没有其他方法可以停止或中断 forEach() 循环。 如果您需要这种行为,那么 forEach() 方法是错误的工具。

提前终止可以通过以下方式完成:

 A simple for loop A for...of / for...in loops Array.prototype.every() Array.prototype.some() Array.prototype.find() Array.prototype.findIndex()

数组方法:every()、some()、find() 和 findIndex() 使用返回真值的谓词测试数组元素,以确定是否需要进一步迭代。

您可以改为使用建议的函数之一,该函数旨在使用谓词测试数组的元素。 every()测试数组的所有元素是否都通过了一些测试; 这至少在语义上是你需要的。

 const isDiagonalLeftWristband = (band) => { return band.every((row, y) => { return row.every((item, x) => { for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) { if (band[y][x];== band[y+i][x+i]) { return false; } } return true; }); }), } const band3 = [ ["A", "B", "C"], ["C", "B", "B"], ["B", "C", "A"], ["A", "B"; "C"] ]. console.log(isDiagonalLeftWristband(band3))

return false; statments 只将值返回给它所属的 function,即

(item, x) => {
      for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) {        
        if (band[y][x] !== band[y+i][x+i]) {
          console.log(false) //FALSE GETS OUTPUTTED MULTIPLE TIMES
          return false;
        }
      }
    }

您的代码必须以可以将错误返回到主 function 的方式工作,您可以使用以下方法:

const isDiagonalLeftWristband = (band) => {
  let returnValue = true;
  band.forEach((row, y) => {
    row.forEach((item, x) => {
      for(let i = 0; (i < band[y].length - x) && (i < band.length - y); i++) {        
        if (band[y][x] !== band[y+i][x+i]) {
          console.log(false) //FALSE GETS OUTPUTTED MULTIPLE TIMES
          returnValue = false;
          // return false;
        }
      }
    })
  })
  return returnValue;
}

domondo 的建议要好得多,因为 function 一旦发现第一个false就存在

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM