From logical view it looks fine. You could also try the ternary operator if you want to play around.

return n <= 0 ? 0 : sum(arr, n - 1) + arr[n - 1];

The first block is the if question. If its true is goes to the second block (starts whith ?) and if its false it goes to the third block (starts with :).

Answer from Athii on Stack Overflow
🌐
GitHub
gist.github.com › sanikamal › 488f895ab6be5b9692a893ef12cf57c9
Basic JavaScript: Replace Loops using Recursion · GitHub
Basic JavaScript: Replace Loops using Recursion. GitHub Gist: instantly share code, notes, and snippets.
Discussions

Explanation for the given example for Replace Loops with Recursion
Tell us what’s happening: Describe your issue in detail here. Unfortunately, my issue is with the lesson itself, and how it’s very unclear and inadequate, especially for a concept that is a bit mind-boggling to come across for the first time (A function that calls itself???). First line: ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
1
July 4, 2022
Basic JavaScript - Replace Loops using Recursion
Everything was going great until “recursion” came into play and i think this was where things got mixed up for me hence why the countDown exercise is frustrating me presently, i have gone thru the breakdown explanation of recursion there but the correlation of how they make recursion possible ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
June 11, 2023
Replace loop using recursion
Tell us what’s happening: I think the code is buggy, since i am using recursion and it is still asking me to use recursoin in order to get the lesson. by the way thanks to the creators of this site for including extra lessons. Your code so far js function sum(arr, n) { // Only change code ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
1
October 18, 2019
Replace Loops Using Recursion Clarification
Tell us what’s happening: I understand how basic function recursion works, the function is simply re-called within the function I understand that the function is calling itself and only when n More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
February 7, 2020
Top answer
1 of 3
5

Actually, with n equals to 1 is quite easy. Let's check step by step. Here your sum function; let's add some line to make it easier to refer:

1: function sum(arr, n) {
2:  if(n <= 0){
3:    return 0;
4:  }else {
5:    return sum(arr, n - 1) + arr[n - 1];
6:  }
7: }

Now, let's see what's happening step by step when you execute:

sum([2, 3, 4], 1)

The function sum is called with arr equals to [2, 3, 4], and n equals to 1.

Since n is not less or equals than 0 (line 2), we go to the else block, at line 5.

Now, here is where the recursion happens: we call again the function sum, passing the same arr, but not the same n, instead we pass n - 1.

So we call sum again, this time with arr equals to [2, 3, 4] but with n equals to 0. Since n is 0 this time, at the line 2 check we proceed to line 3 and returns 0.

Now, the function exit, with the value 0, that we gave to the caller.

And the caller of sum([2, 3, 4], 0) was the execution sum([2, 3, 4], 1), specifically at line 5:

5:    return sum(arr, n - 1) + arr[n - 1];

Since it returned 0, we can imaging like:

5:    return 0 + arr[n - 1];

And remember that n is 1, so:

5:    return 0 + arr[0];

Since arr[0] is equals to 2:

5:    return 0 + 2;

And then why sum([2, 3, 4], 1) returns 2.

I'm not sure if it's clearer now, but I hope so. :)

2 of 3
1

sum(arr, n) is a recursive function that returns the sum of the first n elements of an array arr.

In your example, you provide sum([2, 3, 4], 1) which basically says, compute the sum of the first element (i.e. the value of n in this example is 1).

So it would go through the function as such...

// the first time through 
function sum([2, 3, 4], 1) {
  if(1 <= 0){ // false this time
    return 0; 
  }else { // this is where we end up
    return sum([2, 3, 4], 0) + 2; // sum will be the result of the recurse back into the function, plus 2
  }
}

// the second time through 
function sum([2, 3, 4], 0) {
  if(0 <= 0){ // true this time 
    return 0; // send this result back up to the first run through
  }else {
    // not relevant this time
  }
}

// back in the the first time through, 
// we now have a value to work with below
// remember, this isn't the 'third' time through,
// it is back in the first time run through
// just re-printed here so you could see 
// where the value gets returned from the second run
function sum([2, 3, 4], 1) {
  if(1 <= 0){ // false this time
    return 0; 
  }else { // this is where we end up
    return sum(0 + 2); // we got a result from the second run through, sum is now 2
  }
}
🌐
Hashnode
hashnode.com › post › fcc-basic-javascript-replace-loops-using-recursion-ck9uevoq1046v7bs1gsbmop2t
fCC - Basic JavaScript: Replace Loops using Recursion - Hashnode
May 5, 2020 - However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. That means you can rewrite multiply in terms of itself and never need to use a loop. function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } } The recursive version of multiply breaks down like this.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Explanation for the given example for Replace Loops with Recursion - JavaScript - The freeCodeCamp Forum
July 4, 2022 - Tell us what’s happening: Describe your issue in detail here. Unfortunately, my issue is with the lesson itself, and how it’s very unclear and inadequate, especially for a concept that is a bit mind-boggling to come across for the first time (A function that calls itself???). First line: “Recursion is the concept that a function can be expressed in terms of itself.” This very first line is already a bit iffy.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops Using Recursion Clarification - JavaScript - The freeCodeCamp Forum
February 7, 2020 - Tell us what’s happening: I understand how basic function recursion works, the function is simply re-called within the function I understand that the function is calling itself and only when n <= 0 is false, then e…
🌐
Reddit
reddit.com › r/learnjavascript › could anyone kindly help me understand the following 'replace loops using recursion' code.
Could anyone kindly help me understand the following 'Replace Loops using Recursion' code. : r/learnjavascript
February 22, 2022 - So I'm finishing up the basic Javascript section on freeCodeAcademy which appears to be a bit dated and confusing at times due to multiple edits over the years.
🌐
freeCodeCamp
forum.freecodecamp.org › guide
freeCodeCamp Challenge Guide: Replace Loops using Recursion - Guide - The freeCodeCamp Forum
February 22, 2020 - Replace Loops using Recursion Hints Hint 1: When n <= 0 sum(arr, n) returns 0. Hint 2: When n is larger than 0 sum(arr, n) returns sum(arr, n - 1) + arr[n - 1] Solutions: (Click to reveal) function sum(arr, n) { if(…
🌐
Refactoring
refactoring.com › catalog › replaceIterationWithRecursion.html
Replace Iteration with Recursion - Refactoring
(it is, of course, possible to ... version) Identify the candidate loop. The loop should modify one or more scoped locals, and then return a result based on their final values. Move the loop into a new function. Compile and rerun tests. Replace the loop with a function that accepts the local variables, and which returns the final result...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops using Recursion - JavaScript - The freeCodeCamp Forum
October 29, 2023 - function sum(arr, n) { // Only change code below this line if (n
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops using Recursion - Explained - JavaScript - The freeCodeCamp Forum
September 9, 2021 - Hey all, I wanted to share this, as I read through the previous posts and nothing mentioned my problem with understanding how this all works. Here is my code: function sum(arr, n) { // Only change code below this li…
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Loop replacement with recursion - JavaScript - The freeCodeCamp Forum
January 14, 2022 - please what are my missing out on this challenge? **Your code so far** function sum(arr, n) { // Only change code below this line if (n <= 0) { return 0; } else { return sum(arr,n - 1) + [n - 1]; } // Only change co…
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript #103: Replace Loops using Recursion - JavaScript - The freeCodeCamp Forum
June 5, 2022 - function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } } In the base case, where n <= 0 , it returns 1. What is the purpose for, and resul…
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops Using Recursion - Comprehension - JavaScript - The freeCodeCamp Forum
February 19, 2021 - Hello, I have read other posts and watched various videos but am still stumped on comprehending the example code in this exercise. I understand the concept of recursion being a “reflection,” calling itself as a sort of “shortcut” from using loops in order to execute statements until ...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Replace Loops using Recursion Help Please - JavaScript - The freeCodeCamp Forum
May 9, 2020 - I have so many questions here I don’t know where to start. I should say that I have done all the exercises up to this point but otherwise have had absolutely no experience with coding. Therefore it is entirely possible, …
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Basic JavaScript - Replace Loops using Recursion - Page 2 - JavaScript - The freeCodeCamp Forum
July 10, 2023 - Now we need to get the return value for sum([1,3,4,5], 1) (so we can add it to 3), which is: · Now that we know the return value for sum([1,3,4,5], 1) we can use it in the original return statement for sum([1, 3, 4, 5], 2) which now makes that ...