0

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.

6
  • Where exactly did this "tutorial" come from? Commented Sep 24, 2011 at 16:12
  • 1
    offtopic: 'Eloquent JavaScript' is bad book, and very bad tutorial. Just throw it away, and get yourself a copy of Stoyan Stefanov's 'Javascript Patterns' or Crockford's 'Good parts'. Commented Sep 24, 2011 at 16:13
  • I don't know that it's bad, but it's definitely not for beginners. Commented Sep 24, 2011 at 16:50
  • the book is definitly not for beginners, but since I already know all the basic stuff, I want to push myself to understand more. It's pretty lightweight and goes around all the basic and hard stuff. Commented Sep 24, 2011 at 16:57
  • One of the key "basic" things to understand here is recursion. Commented Sep 24, 2011 at 17:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for the quick reply, I'm still having trouble getting over here history: "(((((1 + 5) + 5) + 5) + 5) * 3)" start: 63 history: "((((1 + 5) + 5) + 5) * 3)" start: 48 how does 63 get to 48, since 63 is too big, it gets a null value. while history already cached ((((1 + 5) + 5) + 5) + 5) how can it still remove a +5
I don't know what you mean by that. At every iteration through the function, the code will either add five or multiply by three. It doesn't have to "remove a + 5" because it does not actually change the value of "history" by calling the function again. Each invocation of the function gets a completely fresh "history" parameter of its own; it does not share it with other calls to the same function.
test it by putting a breakpoint on return null. Imma explain my though of process and what I see on the firebug console:when its 21 it adds 5 , 26 (it's too big, so im gonna try to multiply by 3 instead), 3 * 21 = 63, 63 it's still too big, now the break point get stuck at null. And it test with a start value of 48 which I don't get it.
Sorry, I just don't understand your question; specifically, I don't understand the sentence, "And it test with a start value of 48 which I don't get." What do you mean by "test"?
well when at 63 the number is too big, it goest to null and then goes back to if (start == goal) and the local variable history: "((((1 + 5) + 5) + 5) * 3)" start: 48. thanx for your patience, I really appreciate. I have put a break point on if (start == goal), return null; and return find(start + 5, "(" + history + " + 5)") || find(start * 3, "(" + history + " * 3)");
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.