Javascript..完全迷失在本教程中

发布于 2024-10-18 15:38:02 字数 1436 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-10-25 15:38:02

您有一个“输出”字符串 (((1 * 3) + 5) * 3)、目标 24 和第一个字符 111 * 3 中的 1。该程序将组装一个包含数学表达式的字符串,添加 ()、*3 和 +5 以尝试获得目标。 find 函数的两个参数是当前总计和将生成总计的表达式。显然表达式是一个字符串。 find 将当前总数与目标进行比较,如果相等,则表达式正确,并且如果当前总数 > 则返回它。目标失败,然后返回 null。否则他会向表达式添加一些操作。他尝试了两种“方法”,一种将当前结果乘以 * 3,另一种将当前结果加 +5。它在一棵树上递归(所以每次他都会分叉两次递归调用)。空 || Something == Something,因此找到响应的分支将返回他的响应,另一个分支将返回 null,并且“获胜”响应将被传回。

假设目标是 11.find

  • (1, "1")
    1. 比较 1 和 11 并调用: (2.) find(1 + 5, "(" + "1" + " + 5)") (所以 find(6, "(1 + 5)") 和 ( 3.) find(1 * 3, "(" + "1" + " * 3)") (所以 find(3, "(1 * 3)")
    2. 将 6 与 11 进行比较并调用 (4.) find (6 + 5, "(" + "(1 + 5)" + " + 5)") (所以 find(11, "((1 + 5) + 5)") 和 (5.) find (6 * 3, "(" + "(1 + 5)" + " * 3)" (所以 find(18, "((1 + 5) * 3)"
    3. 将 3 与 11 进行比较并调用 (6.) find (3 + 5, "(" + "(1 * 3)" + " + 5)") (so find(8, "((1 * 3)) + 5)") 和 (7.) find (3 * 3, "(" + "(1 * 3)" + " * 3)" (所以 find(8, "((1 + 3) * 3)"
    4. 比较 11 和 11。数字相等。所以他返回“((1 + 5) + 5)”(历史)。 5、6、7 在某个时刻会“超出”并超过 11,因此它们将返回 null。空 || null == null, "((1 + 5) + 5)" || null == "((1 + 5) + 5)",因此历史记录将战胜 null 并返回。

为了使其更清楚,请尝试以下版本

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

: where 而不是表达式,您只能得到 + 和 * 的字符串,

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

并且请注意,作为示例,它非常复杂,因为它使用了闭包(本地定义的函数)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文