diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.english.md index deef538336..fe6b75bc11 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion.english.md @@ -7,7 +7,7 @@ videoUrl: 'https://www.freecodecamp.org/news/how-recursion-works-explained-with- ## Description
-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: multiply the first n elements in an array to create the product of the elements. Using a for loop, you could do this: +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: multiply the elements from 0 to n inclusive in an array to create the product of those elements. Using a for loop, you could do this: ```js function multiply(arr, n) { @@ -31,7 +31,7 @@ However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n]multiply breaks down like this. In the base case, where n <= 0, it returns the result, arr[0]. For larger values of n, it calls itself, but with n - 1. That function call is evaluated in the same way, calling multiply again until n = 0. At this point, all the functions can return and the original multiply returns the answer. +The recursive version of multiply breaks down like this. In the base case, where n <= 0, it returns the result, arr[0]. For larger values of n, it calls itself, but with n - 1. That function call is evaluated in the same way, calling multiply again until n = 0. At this point, all the functions can return and the original multiply returns the answer. 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. @@ -40,7 +40,7 @@ The recursive version of multiply breaks down like this. In the -Write a recursive function, sum(arr, n), that creates the sum of the first n elements of an array arr. +Write a recursive function, sum(arr, n), that returns the sum of the elements from 0 to n inclusive in an array arr.
diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion/index.md new file mode 100644 index 0000000000..1829efd4dc --- /dev/null +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion/index.md @@ -0,0 +1,38 @@ +--- +title: Replace Loops using Recursion +--- + +# Replace Loops using Recursion + +### Hint 1: + +When `n <= 0` `sum(arr, n)` returns `arr[0]`. + +### Hint 2: + +When `n` is larger than 0 `sum(arr, n)` returns `sum(arr, n - 1) + arr[n]` + +## Basic code solution: + +
(Click to reveal) + +```js +function sum(arr, n) { + if (n <= 0) { + return arr[0]; + } else { + return sum(arr, n - 1) + arr[n]; + } +} + +``` + +
+ +### Code explanation + +The `if` statement checks to see if `sum` is evaluating the base case, `n <= 0`, or not. If it is, then `sum` returns the answer, `arr[0]` - the sum of elements from 0 to 0 inclusive. Otherwise it recurses by evaluating a simpler function call, `sum(arr, n - 1)`. Once that returns it adds a single array element, `arr[n]`, to it and returns that sum. + +### Resources + +- [Recursive Functions - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Recursion)