Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3991292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:35:53+00:00 2026-05-20T06:35:53+00:00

My introductory tutorial has suddenly become very advanced. I have no idea how this

  • 0

My introductory tutorial has suddenly become very advanced. I have no idea how this program works. Can you explain in plain language?

At the end it prints (((1 * 3) + 5) * 3), but I don’t get it at all. I understand that findSequence gets passed 24, that triggers function find. I’m assuming that function find gets passed 1,”1″ with the latter being assigned to history?? but I don`t understand why the second 1 is in quotation marks “1!, nor do I understand the use of quotation marks when it returns find(start etc.

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:35:53+00:00Added an answer on May 20, 2026 at 6:35 am

    You have an “output” string (((1 * 3) + 5) * 3), a goal 24 and a first character 1. The 1 is the 1 in 1 * 3. This program will assemble a string containing a math expression, adding (), *3 and +5 to try to obtain the goal. The two parameters of the find function are the current total and the expression that will generate the total. Clearly the expression is a string. The find compares the current total to the goal, and if it’s equal then the expression is correct and it returns it, if the current total is > of the goal then he failed and return null. Otherwhise he add some operations to the expression. He tries two “ways”, one multiplying * 3 the current result, the other adding +5 to the current result. It’s recursive on a tree (so each time he will bifurcate in two recursive calls). null || something == something, so the branch that will find a response will return his response, the other branch will return null and the “winning” response will be passed back.

    Let’s say the goal is 11.

    • find(1, “1”)
      1. compares 1 with 11 and calls: (2.) find(1 + 5, “(” + “1” + ” + 5)”) (so find(6, “(1 + 5)”) and (3.) find(1 * 3, “(” + “1” + ” * 3)”) (so find(3, “(1 * 3)”)
      2. compares 6 with 11 and calls (4.) find (6 + 5, “(” + “(1 + 5)” + ” + 5)”) (so finds(11, “((1 + 5) + 5)”) and (5.) find (6 * 3, “(” + “(1 + 5)” + ” * 3)” (so find(18, “((1 + 5) * 3)”
      3. compares 3 with 11 and calls (6.) find (3 + 5, “(” + “(1 * 3)” + ” + 5)”) (so finds(8, “((1 * 3) + 5)”) and (7.) find (3 * 3, “(” + “(1 * 3)” + ” * 3)” (so find(8, “((1 + 3) * 3)”
      4. compares 11 with 11. The numbers are equal. So he returns “((1 + 5) + 5)” (the history). 5, 6, 7 will at a certain point go “overboard” and surpass the 11, so they’ll return null. null || null == null, “((1 + 5) + 5)” || null == “((1 + 5) + 5)”, so the history will win against the nulls and it will be returned.

    To make it even clearer, try these versions:

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

    And this, where instead of an expression you’ll get only as tring of + and *

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

    And be aware that, as an example, it’s quite complex because it used closures (locally defined functions).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was making this very simple lex program (just an introductory program). But on
I'm following the Django-CMS introductory tutorial and have got everything working up to the
Are there any favorite online references or good introductory and intermediate/advanced books on this
This is probably a very basic/introductory thing, but let me paint a scenario: Let's
Currently I am writing a program for an introductory Java class. I have two
I recently started learning Emacs . I went through the tutorial, read some introductory
I'm reading this introductory book on parsing (which is pretty good btw) and one
As part of an introductory animation on my site, I am attempting to have
I am trying to follow Pete Brown's introductory WPF tutorial which makes use of
I'm working on some introductory recursion problems and I have a clarifying question I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.