javascript
递归javascript_JavaScript中的递归
遞歸javascript
by Kevin Ennis
凱文·恩尼斯(Kevin Ennis)
JavaScript中的遞歸 (Recursion in JavaScript)
I’m just gonna get this out of the way right up front, because people get really angry otherwise:
我只是直接解決這個問題,因為否則人們會非常生氣:
Consider this post as a series of learning exercises. These examples are designed to make you think — and, if I’m doing it right, maybe expand your understanding of functional programming a little bit.
將此帖子視為一系列學習練習。 這些示例旨在讓您思考-如果我做對的話,也許可以使您對函數式編程的理解有所擴展。
嘿,哇 我聽說您喜歡遞歸,所以我說了“嘿,天哪。 我聽說您喜歡遞歸,所以我說了“嘿,天哪…… (Hey, dawg. I heard you like recursion, so I put a “Hey, dawg. I heard you like recursion, so I put a “Hey, dawg…)
Loosely defined, recursion is the process of taking a big problem and sub-dividing it into multiple, smaller instances of the same problem.
松散地定義,遞歸是處理一個大問題并將其細分為相同問題的多個較小實例的過程。
Put into practice, that generally means writing a function that calls itself. Probably the most classic example of this concept is the factorial function.
付諸實踐,通常意味著編寫一個調用自身的函數。 此概念的最經典示例可能是階乘函數。
You may remember from math class that the factorial of a number n is the product of all positive integers less than or equal to n. In other words, the factorial of 5 is 5 x 4 x 3 x 2 x 1. The mathematical notation for this is 5!.
您可能從數學課上還記得數字n的階乘是所有小于或等于n的正整數的乘積。 換句話說,階乘5是5 x 4 x 3 x 2 x 1 。 為此的數學符號為5! 。
Something interesting you might have noticed about that pattern: 5! is actually just 5 x 4!. And 4! is just 4 x 3!. So on and so forth until you get down to 1.
關于該模式,您可能已經注意到了一些有趣的事情: 5! 實際上只有5 x 4! 。 還有4! 只有4 x 3! 。 依此類推,直到您降至1為止。
Here’s how we’d write that in JavaScript:
這是我們用JavaScript編寫的方式:
If this seems confusing, I’d encourage you to mentally walk through the code using the example of factorial( 3 ).
如果這看起來令人困惑,我建議您使用factorial(3)的示例在代碼上仔細地學習 。
Here’s a bit of help, in case you need it:
如果需要的話,這里有一些幫助:
factorial( 3 ) is 3 x factorial( 2 ).
factorial(3)是3 x factorial(2) 。
factorial( 2 ) is 2 x factorial( 1 ).
factorial(2)是2 x factorial(1) 。
factorial( 1 ) meets our if condition, so it’s just 1.
factorial(1)滿足我們的if條件,因此僅為1。
So what’s really happening here is that you’re winding up the call stack, getting down to 1, and then unwinding the stack. As you unwind the call stack, you multiply each result. 1 x 2 x 3 is 6, and that’s your return value.
因此,這里真正發生的事情是結束調用堆棧,降低到1 ,然后展開堆棧。 展開調用堆棧時,將每個結果相乘。 1 x 2 x 3是6 ,這就是您的返回值。
反轉字符串 (Reversing A String)
One of my co-workers recently told me about a whiteboard question that he’d been asked in an interview, and I thought it was kind of a fun problem.
我的一位同事最近告訴我一個關于白板問題的采訪中有人問他,我認為這是一個有趣的問題。
Write a function that accepts a string a reverses it. Recursively.編寫一個接受字符串并將其反轉的函數。 遞歸地。If you’re the ambitious type, I’d encourage you to take a few minutes and try to solve this one on your own. Keep in mind the core principle of recursion, which is to take a big problem and break it down into smaller instances of itself.
如果您是一個有野心的人,我建議您花幾分鐘時間,嘗試自己解決這個問題。 請記住遞歸的核心原則,即解決一個大問題并將其分解為更小的實例。
If you got stuck (or you’re the decidedly unambitious type), here’s my solution:
如果你卡住了(或者你是決然不思進取型),這里是我的解決方案:
Again, I’ll give a quick walk-through example in case you got stuck. We’ll use reverse(‘bar’) as a starting point.
再次,我將給出一個快速的示例,以防您陷入困境。 我們將使用reverse('bar')作為起點。
reverse(‘bar’) is reverse(‘ar’) + ‘b’
reverse('bar')是reverse('ar')+'b'
reverse(‘ar’) is reverse(‘r’) + ‘a’
reverse('ar')是reverse('r')+'a'
reverse(‘r’) meets our if condition, so it’s just ‘r’
reverse('r')滿足我們的if條件,所以它只是'r'
When the call stack unwinds, we end up with ‘r’ + ‘a’ + ‘b’.
當調用堆棧結束時,我們最終得到'r'+'a'+'b' 。
編寫遞歸映射函數 (Writing a Recursive Map Function)
For our final example, we’re going to write a map() function. We want to be able to use it like this:
對于最后一個示例,我們將編寫一個map()函數。 我們希望能夠像這樣使用它:
Again, I’d strongly encourage you to take a few minutes and try this one on your own. Here are a few hints and reminders:
再一次,我強烈建議您花幾分鐘時間,自己嘗試一下。 以下是一些提示和提醒:
map() should always return a new array.
map()應該始終返回一個新數組。
Remember the reverse() example.
記住reverse()示例。
Oh, good. You’re back. How did it go?
哦好 你回來了。 怎么樣了
j/k, this is a blog and I can’t hear you. lol.
j / k,這是一個博客,我聽不到您的聲音。 大聲笑。
Anyway, here’s how I did it:
無論如何,這是我的做法:
So let’s go through this using the example I gave earlier:
因此,讓我們使用我之前給出的示例來完成此操作:
Call map() using the array [ ‘a’, ‘b’, ‘c’ ]
使用數組['a','b','c']調用map()
Create a new array that holds the result of calling fn(‘a’)
創建一個新數組,其中包含調用fn('a')的結果
Return [ ‘A’ ].concat( map([ ‘b’, ‘c’ ]) )
返回['A'] .concat(map(['b','c']))
Repeat steps 1 through 3 with [ ‘b’, ‘c’ ]
用['b','c']重復步驟1至3
Repeat steps 1 through 3 for [ ‘c’ ]
對['c']重復步驟1至3
Eventually, we call map() with an empty array, which ends the recursion.
最終,我們使用一個空數組調用map() ,從而結束了遞歸。
NOTE:You should never, ever, ever do this in a real application. You’ll blow out the stack on large arrays, and more importantly, you create a huge amount of garbage by instantiating so many new objects. Use Array#map in production code.
注意:您永遠不要在真實的應用程序中這樣做。 您將炸毀大型陣列上的堆棧,更重要的是,通過實例化許多新對象來創建大量垃圾。 在生產代碼中使用Array#map 。
結語 (Wrap Up)
Hopefully I did a decent job in explaining this stuff. If you’re still struggling a bit to wrap your head around recursion, the best advice I can give is to start with small examples and mentally trace the call stack. Try something like reverse(‘abc’) and walk through it, step-by-step. Eventually it’ll click.
希望我在解釋這些東西方面做得不錯。 如果您仍在努力地繞過遞歸,那么我可以提供的最佳建議是從小的示例開始,并從心理上跟蹤調用堆棧。 嘗試類似reverse('abc')之類的步驟,并逐步進行操作。 最終它將單擊。
— -
--
Follow me on Twitter or Medium for more posts. I’m trying to write once a day for the next 30 days.
在Twitter或Medium上關注我以獲取更多帖子。 在接下來的30天里,我每天嘗試寫一次。
And if you’re in the Boston area and want to come work on crazy, interesting, hard problems with me at Starry, shoot me an email. I’m hiring.
如果您在波士頓地區,并且想在Starry與我一起解決瘋狂,有趣,棘手的問題,請給我發送電子郵件 。 我在招聘。
翻譯自: https://www.freecodecamp.org/news/recursion-in-javascript-1608032c7a1f/
遞歸javascript
總結
以上是生活随笔為你收集整理的递归javascript_JavaScript中的递归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到儿子发烧了什么意思
- 下一篇: 孕妇梦到火山爆发什么预兆