1

The following simple function takes in an array and a number, and essentially outputs that array's length * the number specified.

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

If we call the following: sum([2, 3, 4, 5], 3), we correctly get 9 as the final output value.
For exmaple:

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))

Not sure I understand what is going on here though. Where is this final value getting stored? I can see that calling the function in the function and using decreasing values in each instance is essentially replacing a for loop, but without additional context I'd expect the output to either always return 0 or for the function to not work at all.

What hidden aspects are coming into play to make this return that desired value?

2
  • what makes this return that desired value is .... return statement Commented Mar 13, 2020 at 20:53
  • 1
    Instead of waiting for good explanation here, go watch some videos on recursion and try to understand it, then take a look at your example again. It might take you some time to grasp it, but you'll get it eventually. Commented Mar 13, 2020 at 20:56

3 Answers 3

1

You could add a logger and watch the algorithm's work.

function sum(arr, n) {
  if (n <= 0) {
    return 0;
  } else {
    console.log(n, arr[n - 1]);
    return sum(arr, n - 1) + arr[n - 1];
  }
}

console.log(sum([2, 3, 4, 5], 3))

Sign up to request clarification or add additional context in comments.

1 Comment

That definitely helps to shed light on what the function is doing, thank you.
1

There are no hidden aspects. This is a classic example of recursion. It has a base case:

if (n <= 0) {
    return 0;
}

... which kicks in when the array has no elements: in that case the sum is indeed zero.

And it is has the recursive case for when the array has one or more elements:

else {
    return sum(arr, n - 1) + arr[n - 1];
}

Here we solve the problem for a shorter array (hence n - 1), and assuming its outcome is correct, we just need to add the value to it that was left out: arr[n - 1]. This is not necessarily zero. When the array has size 1, we get 0 + arr[0] here.

Comments

0

in the first loop when you call sum([2, 3, 4, 5], 3) then your function is returning the following sum([2,3,4,5], 2) + 4

in the second loop when you call sum([2, 3, 4, 5], 2) (which was returned in the first loop) then your function is returning the following sum([2, 3, 4, 5], 1) + 3

in the third loop when you call sum([2, 3, 4, 5], 1) (which was returned in the second loop) then your function is returning the following sum([2, 3, 4, 5], 0) + 2

in the fourth loop when you call sum([2, 3, 4, 5], 0) (which was returned in the third loop) then your function is returning the following 0

so effectively, it is as though you are returning 0 + 2 + 3 + 4

1 Comment

This is a really good explanation for a newbie such as myself. I wasn't considering all of the data that was getting passed through with each iteration so this was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.