Replace Loops using Recursion

Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: Add the first n elements of an array to create the product of those elements. we can rewrite sum in terms…


This content originally appeared on DEV Community and was authored by Randy Rivera

Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: Add the first n elements of an array to create the product of those elements. we can rewrite sum in terms of itself and never need to use a loop.

  • Here we write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.
  • Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0), otherwise they can never finish executing.
function sum(arr, n) {
if (n <= 0) {
  return 0;
} else {
  return sum(arr, n - 1) + arr[n - 1];
}
}
console.log(sum([2, 3, 4, 5], 3)); will display 9
// sum([2, 3, 4, 5], 3 - 1) + arr[3 - 1];
// sum([2, 3, 4 ,5], 2) + arr[2];
// sum([2, 3, 4, 5], 2) + 4;
//        n = 2

// sum([2, 3, 4, 5], 2 - 1) + arr[2 - 1];
// sum([2, 3, 4, 5], 1) + arr[1];
// sum([2, 3, 4, 5], 1) + 3; 
//      n = 1             3

// sum([2, 3, 4, 5], 1 - 1) + arr[1 - 1];
// sum([2  3, 4, 5], 0) + arr[0];
// sum([2, 3, 4, 5], 0) + 2;
//      n = 0             2

//      we hit our base case so now n = 0

// 0 + 2 + 3 + 4 = 9
// we want it to return 9 because 4 + 3 + 2 = 9;


This content originally appeared on DEV Community and was authored by Randy Rivera


Print Share Comment Cite Upload Translate Updates
APA

Randy Rivera | Sciencx (2021-04-23T22:48:13+00:00) Replace Loops using Recursion. Retrieved from https://www.scien.cx/2021/04/23/replace-loops-using-recursion/

MLA
" » Replace Loops using Recursion." Randy Rivera | Sciencx - Friday April 23, 2021, https://www.scien.cx/2021/04/23/replace-loops-using-recursion/
HARVARD
Randy Rivera | Sciencx Friday April 23, 2021 » Replace Loops using Recursion., viewed ,<https://www.scien.cx/2021/04/23/replace-loops-using-recursion/>
VANCOUVER
Randy Rivera | Sciencx - » Replace Loops using Recursion. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/23/replace-loops-using-recursion/
CHICAGO
" » Replace Loops using Recursion." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/04/23/replace-loops-using-recursion/
IEEE
" » Replace Loops using Recursion." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/04/23/replace-loops-using-recursion/. [Accessed: ]
rf:citation
» Replace Loops using Recursion | Randy Rivera | Sciencx | https://www.scien.cx/2021/04/23/replace-loops-using-recursion/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.