如何在JavaScript中理解遞迴?
什麼是遞迴?
遞迴一詞源於“反覆出現”,意味著一次又一次地返回。遞迴函式是一個函式,它透過逐步更改輸入來反覆呼叫自身。這裡,一步一步地更改輸入意味著將輸入減少或增加一步。
每當遞迴函式達到基本條件時,它就會停止自身的執行。讓我們透過一個例子來理解什麼是基本條件。例如,我們需要找到一個數字的階乘。我們透過將輸入減少 1 來呼叫階乘函式,並且每當輸入達到 1 時,我們需要停止。因此,這裡 1 作為基本條件。
語法
使用者可以使用以下語法來理解JavaScript中的遞迴。
function recur(val) { if (base condition) { return; } // perform some action // decrease the value of val by one step return recur(newVal); }
在上面的語法中,使用者可以觀察到,當基本條件為真時,我們返回null以停止函式的執行。如果基本條件為假,我們對輸入值執行某些操作,並再次呼叫recur()函式,並使用新的引數值。
現在,讓我們來看一下遞迴的各種示例。在這裡,我們將首先學習使用for迴圈實現迭代演算法,然後將其轉換為遞迴方法。
示例1(使用for迴圈查詢1到n數字的和)
在下面的示例中,我們編寫了sumOfN()函式來獲取1到N數字的和。我們使用了for迴圈進行N次迭代,並且在每次迭代中,我們將I的值新增到sum變數中。
最後,它返回sum變數的值。
<html> <body> <h3>Using the <i> iterative approach </i> to find sum of n numbers in JavaScript</h3> <div id = "content"> </div> <script> let content = document.getElementById('content'); // function to find the sum of n numbers using an iterative approach function sumOfN(n) { let sum = 0; for (let i = n; i >= 1; i--) { sum += i; } return sum; } content.innerHTML += "The sum of 1 to 10 numbers is " + sumOfN(10) + "<br>"; content.innerHTML += "The sum of 1 to 20 numbers is " + sumOfN(20) + "<br>"; </script> </body> </html>
在上面的示例中,我們使用了迭代方法來查詢N個數字的和。現在,我們將使用遞迴方法來執行相同的操作。
示例2(使用遞迴函式查詢1到n數字的和)
在下面的示例中,sumOfN()函式是一個遞迴函式。我們透過將引數的值減少1來重複呼叫sumOfN()函式。sumOfN(N1)返回N-1個數字的和,我們向其中新增N以獲得N個數字的和。每當N的值變為1時,它返回1,這作為停止函式執行的基本條件。
<html> <body> <h3>Using the <i> recursive approach </i> to find sum of n numbers in JavaScript</h3> <div id = "content"> </div> <script> let content = document.getElementById('content'); // function to find the sum of n numbers using a recursive approach function sumOfN(n) { // base condition if (n == 1) { return 1; } // call function recursively by decreasing the value of n by 1. return n + sumOfN(n - 1); } content.innerHTML += "The sum of 1 to 10 numbers is " + sumOfN(10) + "<br>"; content.innerHTML += "The sum of 1 to 20 numbers is " + sumOfN(20) + "<br>"; </script> </body> </html>
讓我們瞭解上面的遞迴函式是如何工作的。在下面,使用者可以逐步瞭解遞迴函式呼叫是如何發生的。
sumOfN(5); return 5 + sumOfN(4); return 4 + sumOfN(3); return 3 + sumOfN(2); return 2 + sumOfN(1); return 1; return 2 + 1; return 3 + 3; return 4 + 6;
示例3(迭代方法合併陣列的所有字串)
在下面的示例中,我們建立了字串陣列。我們建立了mergeString()函式,用於將陣列的所有字串合併到單個字串中。我們使用for迴圈迭代陣列並將所有字串逐一合併到“str”變數中。
<html> <body> <h3>Using the <i> iterative approach </i> to merge all strings of the array in JavaScript</h3> <div id = "content"> </div> <script> let content = document.getElementById('content'); // function to merge all strings of the array using for loop function mergeString(arr) { let str = ''; for (let i = 0; i < arr.length; i++) { str += arr[i]; } return str; } let arr = ['I', ' ', 'am', ' ', 'a', ' ', 'programmer']; content.innerHTML += "The original array is: " + arr + "<br>"; content.innerHTML += "After merging all strings of the array into the single string is " + mergeString(arr) + "<br>"; </script> </body> </html>
示例4(遞迴方法合併陣列的所有字串)
在下面的示例中,我們將mergeString()函式轉換為遞迴函式。我們獲取陣列的第一個元素,並將其與mergeString()函式返回的結果合併。mergeString()函式在合併後返回最後n-1個數組元素。此外,我們使用slice()方法從陣列中刪除第一個元素。
當陣列中只剩下一個元素時,它返回相同的元素,這作為基本條件。
<html> <body> <h3>Using the <i> Recursive approach </i> to merge all strings of the array in JavaScript</h3> <div id = "content"> </div> <script> let content = document.getElementById('content'); // function to merge all strings of the array using recursion function mergeString(arr) { // based condition if (arr.length == 1) { return arr[0]; } // remove the first element from the array using the slice() method. return arr[0] + " " + mergeString(arr.slice(1)); } let arr = ["I", "am", "a", "web", "developer"]; content.innerHTML += "The original array is: " + arr + "<br>"; content.innerHTML += "After merging all strings of the array into the single string is " + mergeString(arr) + "<br>"; </script> </body> </html>
使用者應該使用哪種方法,迭代還是遞迴?
主要問題是哪種方法更好,迭代還是遞迴,使用者應該使用哪種方法。
在某些情況下,迭代方法比遞迴方法更快。此外,遞迴比迭代佔用更多記憶體。對於某些演算法(如分治法),遞迴更有用,因為我們需要使用遞迴方法編寫更少的程式碼。此外,如果基本條件沒有在遞迴方法中觸發,使用者可能會遇到記憶體洩漏問題。
如果我們可以將程式碼分解成更小的部分,我們應該使用遞迴方法,為了提高程式碼的效能,我們應該使用迭代方法。