使用||运算符在函数

时间:2015-10-15 22:26:23

标签: javascript function recursion

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

这是代码

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){}区块开始的地方。当该代码被执行时,然后要求它返回null。那时historyString = (((((1 + 5) + 5) + 5) + 5) + 5)

我的问题是什么创建跳转到elseBlocks返回语句的另一部分* 3之后的||而不是+ 5.是因为前一次返回为空。或者是因为现在开始>比目标。

先谢谢。

1 个答案:

答案 0 :(得分:1)

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

  

当逻辑表达式从左到右进行评估时,它们会使用以下规则进行可能的“短路”评估测试:

     
      
  • false&& (任何)短路评估为假。
  •   
  • true || (任何)短路评估为真。
  •   
     

MDN)功能

因此,在以下代码段中,如果第一个find()未评估为真值,那么它将执行并返回第二个find()

return find(start + 5, "(" + history + " + 5)") ||
       find(start * 3, "(" + history + " * 3)");
相关问题