我试图更好地理解 IF 语句中的条件。当我更改条件的顺序时,我收到未定义的 TypeError。
当订单更改为:
if (col === maze[row].length || row < 0 || col < 0 || row === maze.length) {
return
}
在 IF 函数中使用 OR 运算符时,比较顺序是否重要?当订单的书写方式不同时,是什么导致了 TypeError
?
工作代码库:
const maze = [
[' ', ' ', ' ', '*', ' ', ' ', ' '],
['*', '*', ' ', '*', ' ', '*', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', '*', '*', '*', '*', '*', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', 'e'],
];
const solve = (maze, row = 0, col = 0, path = "") => {
if (row < 0 || col < 0 || row === maze.length || col === maze[row].length) {
return
}
// Base case
if (maze[row][col] === "e") {
return console.log(`Solved at (${row}, ${col})! Path to exit: ${path}`)
// General case
} else if (maze[row][col] === "*") {
return
}
// Marker
maze[row][col] = "*"
// Right
solve(maze, row, col + 1, path.concat("R"))
// Down
solve(maze, row + 1, col, path.concat("D"))
// Left
solve(maze, row, col - 1, path.concat("L"))
// Up
solve(maze, row - 1, col, path.concat("U"))
// Remove marker
maze[row][col] = " "
}
console.log(solve(maze));
最佳答案
Does the order of the comparisons matter when using OR operators in IF statements?
是的,除了运算符优先级,还需要看associativity和 short-circuit evaluation . ||
运算符具有从左到右 的关联性,这意味着它将从左到右计算表达式。短路评估意味着一旦结果已知,将忽略进一步的逻辑条件。
What is causing the TypeError when the order is written differently?
看你的情况:
col === maze[row].length || row < 0 || col < 0 || row === maze.length
因为逻辑运算是从左到右求值的,所以第一个求值的是col === maze[row].length
。当 row === maze.length
时,col === maze[row].length
的计算结果为 col === undefined.length
,这当然会产生错误。
要解决此问题,您需要在之后首先确认索引不会越界后运行此条件。一个简单的方法是:
row < 0 || col < 0 || row === maze.length || col === maze[row].length
现在,如果前三个条件中的任何一个为 true
,则 JavaScript 不会计算其余条件,因为它已经知道结果为 true
。因此,它不会再崩溃。
(请记住 true || false === true
,所以一旦您看到 true ||
,您甚至不需要阅读表达式的其余部分知道结果将为 true
。)
请注意,如果您使用的语言不使用短路评估,那么您将不得不使用多个 if
语句来运行您的条件正确的顺序:
if (row < 0 || col < 0 || row === maze.length) {
return
}
if (col === maze[row].length) {
return
}
我经常发现自己开始编写这样的代码时,我会仔细考虑检查需要发生的顺序,然后将其简化为一个表达式。
希望对您有所帮助!
关于javascript - 在 IF 函数中使用 OR 运算符时,比较条件的顺序是否重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62105898/