开发者

javascript closure tutorial from eloquent javascript

开发者 https://www.devze.com 2023-04-07 12:31 出处:网络
the question is pretty similar to this thread Javascript..totally lost in this tutorial. fu开发者_开发百科nction findSequence(goal) {

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

    fu开发者_开发百科nction 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.


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.


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!"

0

精彩评论

暂无评论...
验证码 换一张
取 消