javascript - 使用||函数返回值中的运算符

标签 javascript function recursion

我试图理解http://eloquentjavascript.net/03_functions.html第3章中的这个例子。免费在线书籍。我对 || 的使用感到困惑if 语句中最后一个 else 的返回函数中的运算符。

这是代码

function findSolution(target) {
  function find(start, history) {
    if (start == target){
      console.log("-------ifBlock-------");
      console.log("startInteger = " + start + " == targetInteger = " + target + " historyString " + history); 
      return history;
    }
    else if (start > target){
      console.log("-------elseIfBlock-------");
      console.log("startInteger = " + start + " > targetInteger = " + target + " historyString " + history); 
      return null;
    }
    else{
      console.log("-------elseBlock-------");
      console.log("startInteger = " + start + " historyString = " + history);
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)");
    }
  }
  return find(1, "1");
}

findSolution(24);

这是我一直在尝试理解返回流程的所有console.logs ||运算符。

-------elseBlock-------
startInteger = 1 historyString = 1
-------elseBlock-------
startInteger = 6 historyString = (1 + 5)
-------elseBlock-------
startInteger = 11 historyString = ((1 + 5) + 5)
-------elseBlock-------
startInteger = 16 historyString = (((1 + 5) + 5) + 5)
-------elseBlock-------
startInteger = 21 historyString = ((((1 + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 26 > targetInteger = 24 historyString = (((((1 + 5) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 63 > targetInteger = 24 historyString = (((((1 + 5) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 48 > targetInteger = 24 historyString = ((((1 + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 33 > targetInteger = 24 historyString = (((1 + 5) + 5) * 3)
-------elseBlock-------
startInteger = 18 historyString = ((1 + 5) * 3)
-------elseBlock-------
startInteger = 23 historyString = (((1 + 5) * 3) + 5)
-------elseIfBlock-------
startInteger = 28 > targetInteger = 24 historyString = ((((1 + 5) * 3) + 5) + 5)
-------elseIfBlock-------
startInteger = 69 > targetInteger = 24 historyString = ((((1 + 5) * 3) + 5) * 3)
-------elseIfBlock-------
startInteger = 54 > targetInteger = 24 historyString = (((1 + 5) * 3) * 3)
-------elseBlock-------
startInteger = 3 historyString = (1 * 3)
-------elseBlock-------
startInteger = 8 historyString = ((1 * 3) + 5)
-------elseBlock-------
startInteger = 13 historyString = (((1 * 3) + 5) + 5)
-------elseBlock-------
startInteger = 18 historyString = ((((1 * 3) + 5) + 5) + 5)
-------elseBlock-------
startInteger = 23 historyString = (((((1 * 3) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 28 > targetInteger = 24 historyString = ((((((1 * 3) + 5) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 69 > targetInteger = 24 historyString = ((((((1 * 3) + 5) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 54 > targetInteger = 24 historyString = (((((1 * 3) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 39 > targetInteger = 24 historyString = ((((1 * 3) + 5) + 5) * 3)
-------ifBlock-------
startInteger = 24 == targetInteger = 24 historyString = (((1 * 3) + 5) * 3)

我迷路的地方就是 else if (start > target){} 的地方 block 开始。当该代码被执行时,它会被要求返回 null。那时historyString = (((((1 + 5) + 5) + 5) + 5) + 5)

我的问题是什么导致跳转到 elseBlocks return 语句的其他部分(|| 之后的 * 3)而不是+ 5。是不是因为之前返回的是null。或者是因为现在的开始时间 > 目标时间。

提前致谢。

最佳答案

||logical operator 。这意味着如果可以转换为true,它将返回第一个表达式;否则,它返回第二个表达式。

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

  • false && (anything) is short-circuit evaluated to false.
  • true || (anything) is short-circuit evaluated to true.

(MDN)

因此,在下面的代码片段中,如果第一个 find() 计算结果不为真值,那么它将执行并返回第二个 find()

return find(start + 5, "(" + history + " + 5)") ||
       find(start * 3, "(" + history + " * 3)");

关于javascript - 使用||函数返回值中的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33159574/

相关文章:

javascript - 如何在特定的 HTML 标签后剪切字符串?

javascript - PHP 输出未出现在 HTML 中

javascript - 在 JavaScript 中处理具有相同 ID 的两个元素

php - 通过函数运行查询会使程序变慢

c++ - 如何避免 C++ 类模板中的无限递归

c# - 递归对象树的最简单方法

java - 我的程序现在正在运行这个错误?

javascript - 判断一个多词字符串是否包含一个词

c - 如何从 C 函数调用 Objective-C 函数

c - C 中的指针递归