Javascript ..在本教程中完全丢失了 [英] Javascript..totally lost in this tutorial

查看:88
本文介绍了Javascript ..在本教程中完全丢失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的入门教程突然变得非常先进。我不知道这个程序是如何工作的。你能用简单的语言解释一下吗?

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

最后打印(((1 * 3)+ 5)* 3),但我根本没有得到它。据我所知,findSequence传递了24,触发函数find。我假设函数find被传递1,1,后者被分配给历史?但我不明白为什么第二个1用引号1!,当我返回find时也不理解引号的使用(开始等等)

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


推荐答案

你有一个输出string (((1 * 3)+ 5)* 3),目标 24 和第一个字符 1 1 1 * 3 中的1>这个程序将汇编一个包含数学表达式的字符串,添加(),* 3和+5以试图获得目标。 find 函数的两个参数是目前tal和将产生总数的表达式。显然,表达式是一个字符串。查找将当前总数与目标进行比较,如果它相等则表达式是正确的并返回它,如果当前总数>目标,则失败并返回null。另外,他在表达式中添加了一些操作。他尝试了两种方式,一种是将当前结果乘以* 3,另一种是将+5加到当前结果中。它在树上是递归的(因此每次他将在两个递归调用中分叉)。 null ||某事==某事,所以发现响应的分支将返回他的响应,另一分支将返回null并且获胜响应将被传回。

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.

假设目标是11。


  • find(1,1)
  • find(1, "1")

  1. 将1与11进行比较并调用:(2。)find(1 + 5,(+1++ 5))(因此找到(6,(1 + 5))和(3。 )find(1 * 3,(+1+* 3))(所以找(3,(1 * 3))

  2. 将6与11进行比较并调用(4.)find(6 + 5,(+(1 + 5)++ 5))(因此找到(11,((1 + 5)+ 5))和( 5.)找到(6 * 3,(+(1 + 5)+* 3)(所以找到(18,((1 + 5)* 3)

  3. 将3与11进行比较并调用(6.)find(3 + 5,(+(1 * 3)++ 5))(所以找到(8,((1 * 3) )+ 5))和(7.)找到(3 * 3,(+(1 * 3)+* 3)(所以找(8,((1 + 3)* 3)

  4. 将11与11进行比较。数字相等。所以他返回((1 + 5)+ 5)(历史).5,6,7将确定点落水并超过11,所以th ey将返回null。 null || null == null,((1 + 5)+ 5)|| null ==((1 + 5)+ 5),因此历史记录将赢得空值并将返回。

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

这篇关于Javascript ..在本教程中完全丢失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆