javascript closure tutorial from eloquent javascript

假如想象 提交于 2019-12-12 05:39:48

问题


the question is pretty similar to this thread Javascript..totally lost in this tutorial.

    function findSequence(goal) {
      function find(start, history) {
        if (start == goal)
          return history;
        else if (start > goal)
          return null;
        else
          return find(start + 5, "(" + history + " + 5)") ||
                 find(start * 3, "(" + history + " * 3)");
      }
      return find(1, "1");
    }

    print(findSequence(24));

I got stuck at this part :

    find(start * 3, "(" + history + " * 3)"); 

each time start goes beyond goal what does it do? it says it return null but when I test and put breakpoint on

    if (start == goal) it shoes this on the console

    history: "(((((1 + 5) + 5) + 5) + 5) + 5)"
    start: 26

    history: "(((((1 + 5) + 5) + 5) + 5) * 3)"
    start: 63

it add up *3 and take off +5, I don't understand how.


回答1:


The return statement:

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

is an expression involving the "||" operator. That operator will cause the left-hand side to be evaluated. If the result of that is not null, zero, false, or the empty string, then that value will be returned. If it is one of those "falsy" values, then the second expression is evaluated and returned.

In other words, that could be re-written like this:

       var plusFive = find(start + 5, "(" + history + " + 5)");
       if (plusFive !== null)
         return plusFive;
       return find(start * 3, "(" + history + " * 3)")

If "start" ever exceeds "goal", the function returns null. Of course, if both the alternatives don't work, then the whole thing will return null.




回答2:


The expression:

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

Will first attempt to evaluate:

find(start + 5, "(" + history + " + 5)")

If the returned value is not null, 0, false, or the empty string then the statement evaluates to the returned value. If the returned value is null, 0, false, or the empty string then the following will be evaluated next:

find(start * 3, "(" + history + " * 3)")

If the returned value is not null, 0, false, or the empty string, then the statement evaluates to the returned value. If the returned value is null, 0, false, or the empty string, then the statement evaluates to null, 0, false, or the empty string (whichever was returned by the *3 function call).

So the line:

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

is like saying "I'm going to try to find the solution by guessing that I add 5 at this step, and if that doesn't work I'll try to multiply by 3 at this step, and if that doesn't work I give up!"



来源:https://stackoverflow.com/questions/7540111/javascript-closure-tutorial-from-eloquent-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!