最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Using Recursion - Stack Overflow

matteradmin8PV0评论

I came across this problem on FreeCodeCamp (link to the problem below) and I was wondering if someone could help me better understand why this equals 2 when calling with sum([2, 3, 4], 1); I sat and looked at this for a bit but just feel blocked mentally in understanding how this works. Simple developer trying to understand Recursion in Javascript.

Any help would be much appreciated! Thanks internet!

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

Here is a link to the problem:

I came across this problem on FreeCodeCamp (link to the problem below) and I was wondering if someone could help me better understand why this equals 2 when calling with sum([2, 3, 4], 1); I sat and looked at this for a bit but just feel blocked mentally in understanding how this works. Simple developer trying to understand Recursion in Javascript.

Any help would be much appreciated! Thanks internet!

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

Here is a link to the problem: https://www.freecodecamp/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

Share Improve this question asked Apr 14, 2020 at 22:16 Alan RichardsAlan Richards 2835 silver badges18 bronze badges 3
  • 1 In the first iteration, sum(arr, n - 1) is 0 because n - 1 is 0. And arr[n - 1] is 2. – user5734311 Commented Apr 14, 2020 at 22:20
  • Because you call sum with 1 as value of n, so it just picks only the first element from the array. – Igor Nikolaev Commented Apr 14, 2020 at 22:22
  • Quite short run first recursion ends with n==0 and returns also zero plus first array element so 0+2. – Jan Commented Apr 14, 2020 at 22:23
Add a ment  | 

3 Answers 3

Reset to default 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. :)

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, pute 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
  }
}

sum(arr, 1-1) evaluates to 0 because of the if statement that checks if n is <=0

and

arr[n-1] is equal to 2 as you are essentially trying to access the arr[1-1] which is arr[0].

Therefore, it is returning 2 because return sum(arr, n-1) + arr[n-1]; evaluates to 2.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far