问题
I am trying to understand this example in chapter 3 of the http://eloquentjavascript.net/03_functions.html free online book. I am confused about the use of || operator in the return function of the final else in a if statement.
Here is the code
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);
Here is all the console.logs I have been getting to try and understand the flow of the return || operator.
-------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)
Where I get lost is right where the else if (start > target){} block starts. When that code gets executed it is then asked to return null. And at that point the historyString = (((((1 + 5) + 5) + 5) + 5) + 5).
My Questions is what creates the jump to the other part of the elseBlocks return statement the * 3 after the || instead of the + 5. Is it because the previous return was null. Or is it because start is now > than the target.
Thanks in Advance.
回答1:
|| is a logical operator. This means that it will return the first expression if it can be converted to true; otherwise, it returns the second expression.
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)
So in the following snippet, if the first find() doesn't evaluate to a truthy value, then it will execute and return the second find().
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
来源:https://stackoverflow.com/questions/33159574/using-the-operator-in-the-a-return-value-of-a-function