来自Eloquent Javascript的Javascript递归 [英] Javascript recursion from Eloquent Javascript

查看:111
本文介绍了来自Eloquent Javascript的Javascript递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是来自 Eloquent Javascript 。我试图弄清楚这段代码是如何实际执行的。所以我们试图通过添加5或乘以3来找到达到目标数的方法,并且我们从数字1开始。因此,基本上,我们试图找到一系列这样的加法和乘法来产生目标数?例如,可以通过首先乘以3然后再加5来两次来达到数字13,而根本不能达到数字15.

So this is from the Eloquent Javascript. I am trying to figure out how this code is actually stepped through. So we are trying to find a way to reach the target number by adding either 5 or multiplying by 3, and we start from the number 1. So, essentially, we are trying to find a sequence of such additions and multiplications that produce the target number? For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice, whereas the number 15 cannot be reached at all.

这是代码片段:

function findSolution(target) {
  function find(start, history) {
    if (start == target)
      return history;
    else if (start > target)
      return null;
    else
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)");
  }
  return find(1, "1");
}

结果:

console.log(findSolution(24));// → (((1 * 3) + 5) * 3)

显然当我们调用 findSolution(13)时会发生这种情况:

Apparently this is what happens when we call findSolution(13):

为了更好地理解这个函数如何产生我们正在寻找的效果,让我们看一下在搜索数字13的解决方案时所做的所有调用。

To better understand how this function produces the effect we’re looking for, let’s look at all the calls to find that are made when searching for a solution for the number 13.

find(1, "1")
  find(6, "(1 + 5)")
    find(11, "((1 + 5) + 5)")
      find(16, "(((1 + 5) + 5) + 5)")
        too big
      find(33, "(((1 + 5) + 5) * 3)")
        too big
    find(18, "((1 + 5) * 3)")
      too big
  find(3, "(1 * 3)")
    find(8, "((1 * 3) + 5)")
      find(13, "(((1 * 3) + 5) + 5)")
        found!

问题:

1)我很困惑当我们调用findSolution时会发生什么13)。当我没有提供开始或历史编号时,第一行是如何找到(1,1)的?代码如何逐步下线:

Question:
1) I am confused what happens when we call findSolution(13). How is it that the first line is find (1, "1") when I haven't supplied it either the start or history number? How does the code just step down to line:

return find(1,"1")

代码是如何实现的?

2)这是OR语句在递归中的运作方式吗?在递归检查中,Javascript通常会在处理第二个分支之前探索OR语句的第一个分支吗?是这一行:

How does the code get there?
2) Is this how OR statements operate in recursion? In a recursive check, does Javascript typically explore the first branch of the OR statement before tackling the second branch? Is this line:

find(3, "(1 * 3)")  

仅在所有分支后检查:

find(6, "(1 + 5)")

无法返回解决方案?

推荐答案

1)如果查看代码,函数findSolution由两部分组成:方法定义find ,并调用该方法返回find(1,1)。除非被调用,否则定义本身不会产生计算。因此,实际执行的第一个表达式是find(1,1)。这是起点。

1) If you look at the code, the function "findSolution" is composed of two parts: a method definition "find", and a call to that method "return find(1, "1")". The definition, in itself, won't produce computing unless it is called. So, the first expression that is actually executed is "find(1, "1")". It's the starting point.

2)OR语句按其阅读顺序执行,因此首先执行find(start + 5,...),并且那么只有当它确实返回null(也就是说,它没有找到解决方案)时,才会执行下一个语句find(start * 3,...)。由于递归,您可以在执行* 3之前执行任意数量的+5执行,具体取决于目标。

2) OR statements are executed in their reading order, so "find(start + 5, ...)" will be first executed, and then only if it does return null (that is, it hasn't find the solution) the next statement "find(start * 3, ...)" is going to be executed. Due to recursion, you can have any number of "+5" exection before a "*3" being executed, depending on the target of course.

这篇关于来自Eloquent Javascript的Javascript递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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