【发布时间】:2016-01-14 13:30:35
【问题描述】:
我试图在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){} 块的开始。当该代码被执行时,它会被要求返回 null。那时historyString = (((((1 + 5) + 5) + 5) + 5) + 5)。
我的问题是创建跳转到 elseBlocks return 语句的另一部分的原因,即 || 之后的 * 3而不是+ 5。是不是因为之前的返回为null。还是因为 start 现在 > 而不是 target。
提前致谢。
【问题讨论】:
-
这是因为 elseIfBlock 总是返回 null,所以
null || func()导致调用func()
标签: javascript function recursion