Recursive functional method produces some pretty amazing results

Here’s a method taken from ‘Eloquent JavaScript’


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

var z = findSequence(24);
spew(z);

(((1 * 3) + 5) * 3)

That’s amazing!

Advertisements

About SunKing2

Former web developer, guitar player, started Piano 2012 -yes still doing it! Recovery, sobriety, mental health advocate.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s