4

I am stuck on the following function which appears in a few other posts I've also reviewed.

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));

Also given in this link.

Javascript..totally lost in this tutorial

In the above explanation, the answer instead tried to set a goal of 11. They have a start of 1, which is first tested against 11, and then a start of 6 which is tested against 11.

I understand these first two steps. However, I do not understand the leap from the 2nd step (comparing start:6 to goal:11) to the third step (comparing start:3 to goal:11).

How does start go from 6, back down to 3, and then back up to 11 (fourth bullet)?

6
  • English is my first language. Commented Oct 27, 2012 at 3:08
  • This explanation in the post is very useful, which is why I provided the link instead. Commented Oct 27, 2012 at 3:09
  • 1
    check this answer - maybe it can clarify. stackoverflow.com/questions/7540111/… Commented Oct 27, 2012 at 3:17
  • c69, can you try to explain my question. I've also reviewed that post. Commented Oct 27, 2012 at 3:19
  • 1
    the *3 is only evaluated if the left-hand side of || evaluates to false. For example type "" || "aaa" in your JS console and then type "bbb" || "ccc" Commented Oct 27, 2012 at 3:39

1 Answer 1

7

Ok, here is a version of the code which was enhanced with console log statements. Open Chrome/Opera/Firefox eveloper tools and execute this code there:

function findSequence (goal) {
  function find (start, history, depth) {
    depth = depth || 0;
    console.log( Array( ++depth ).join('--> '), start, goal, history );
    if (start == goal) {
      console.warn( 'history' );
      return history;
    } else if (start > goal) {
      console.error( 'null' );
      return null;
    } else {
      console.info('recursion!');
      return find(start + 5, "(" + history + " + 5)", depth) ||
             find(start * 3, "(" + history + " * 3)", depth);
    }
  }
  return find(1, "1");
}

console.info( findSequence(24) );

You will get a call trace of this program, and hopefully will grasp the concept of recursion visually, by looking on the trace.

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

3 Comments

I agree this will help him understand concept but by his comments I'd say his confusion lies in the ||.
That is exactly what I needed to see and answers my question perfectly. Thank you very much.
pedrofurla, so, since the left side eventually returns null, it's is false, and triggers the right side to begin. Correct?

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.