An unclear thing about one challenge

When n <= 0 sum(arr, n) returns 0 .

What I don’t understand is why should sum be equal to zero, if an array can be non-zero? Sure, n is 0 in this case… but is it really a sum if an array is, actually, say 5? Wouldn’t sum be 0+5=5 in that case? Here’s a link to the challenge:

1 Like

sum(arr, n) here, n refers to the index of arr…

when n==1 it’ll return first element + zero(n-1==0), thus terminating the recursion

Yes, in the end the sum will be 0 + 5 = 5. Only the case when n will get to zero or less will return 0 but this will be summed up with the rest.

Let’s say we have an array [1, 2, 3] and we pass it to our function sum(arr, n) like so sum([1, 2, 3], 2), so we want to get a sum of first two elements.

function sum(arr, n) {
  if (n <= 0) {
    return 0;
  } else {
    // Here sum function calls itself again (recursively) but with
    // n decreased by 1. So whatever sum(arr, n - 1) will return 
    // it will be added to final result 
    return sum(arr, n - 1) + arr[n - 1];

This is step by step how it would work.

sum([1,2,3], 2)     
sum([1,2,3], 1) + 2
(sum([1,2,3], 0) + 1) + 2
0 + 1 + 2 

I hope you get what I mean :slight_smile:


This was a perfect explanation! I had the right answer, but was still not getting how it worked. This made everything click. Thanks!

could you write the explanation with ([2,3,4,5],3)

why don’t you try it yourself? we can tell you how well you are doing

1 Like

this explained it well - thank you

Hi @ieahleen…I’m going to try!

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

for the base case, where n<=0, it returns 0 - so whenever n gets to this point, it returns zero and stops.
for larger values of n (that is, greater than or equal to 1) the function calls itself but with n-1 and it will keep doing this until n is equal to or less than zero.

sum ([2,3,4,5] , 3) //
n here is ‘3’ so does not meet the if statement and goes straight into the condition where it starts to recurse itself
sum([2,3,4,5] , 2) + 2 // i.e. "sum([2,3,4,5], n-1) + sum[n-1] ";
here the first part is looking for the sum of the first 2 elements of the array (which equals 5) and the second part it calls itself but as [n-1] (ie [3-1] ) which equals 2…thus result here so far is 5 + 2 = 7
then it continues to call itself
(sum([2,3,4,5] , 1) + 1) + 2 // here it is looking for the ‘sum’ of the first element in the array (which equals 2) , then adds the previous two results (ie the +1 and +2)
result here is 2 + 7 = 9
and it still continues to recurse itself…
(sum(([2,3,4,5],0) +0) + 1) + 2 // here n is equal to 0 so this will automatically return a 0 as it meets the first if statement…so here, it stops as it has reached the base case.

thus, the total (final result) is 7 + 2 + 0 = 9

is that right?
this explanation doesn’t fit with @sitek94 though so I’m guessing I’ve just somehow made it fit without really understanding what’s happening
with this explanation it appears (to me) that the ‘n’ value in the function’s parenthesis is selecting the relevant index of the array in that parenthesis:

sum( [1,2,3] , 2). //result is 2. (because n=2 and is choosing the 2nd index, which is 2)
sum( [1,2,3], 1) + 2. //result is 1 + 2. (because n=1 and is choosing the 1st index)
sum( ([1,2,3],0) +1) +2. //result is 0 + 1 + 2. (because n=0, it automatically returns 0)
thus 0 + 1 + 2 = 3.

if I apply that logic to the other function:

sum( [2,3,4,5], 3) //result is 4 (because n = 3 and is choosing the 3rd index, which is 4)
sum( [2,3,4,5], 2) + 2 //result is 3 + 4. (because n = 2 and is choosing the 2nd index, ie 3)
sum (( [2,3,4,5], 1) + 1) + 2 //result is 2 + 3 + 4
sum ((( [2,3,4,5], 0) + 1) + 1) + 2. //result is 0 + 2 + 3 + 4

final result /total = 9

same result either way but I don’t know which is the correct logic!
I’ve tried researching online but I’m feeling confused…

I hope how I’ve explained my process makes sense

would love some feedback from anyone who happens to read this reply

you get to the right result but you are missing what the single things mean

this is not correct
the return statement of sum([2,3,4,5],3) is sum([2,3,4,5],2 + arr[2] and arr[2] is equal to 4
so that line is sum([2,3,4,5],2) + 4

see if you can correct the other steps on your own

1 Like

I have had the same problem but my first approach was using another approach where you actually start counting numbers from 0,1,2,3 so this will equal to [2,3,4,5]. This does not add up but it sounds more logical since we are dealing with arrays. What do you think???

ok… I think I’ve got it now (if I’m right, what I was missing is really obvious now)

sum ([ 2, 3, 4, 5] , 3)
return sum ([ 2, 3, 4, 5] , 2) + arr[2] //result is 5 + 4 (because the 2nd index of arr is 4)
sum (([2,3,4,5], 1) + arr[1]) + 4 //result is 2 + 3 + 4
sum ((([2,3,4,5], 0) + arr[0]) + 3 + 4 // result is 0 (bec n=0) + 2 + 3 + 4

which is 9!!

@ieahleen: can you please tell me if I have understood this correctly ?
ps. Your response earlier was so good - it made me stop and really try to work it out, it pushed me and I appreciate that (despite not saying so at the time)

for me, if I finally got it right, it came down to the arr[ n-1] part.
I was totally misunderstanding that part, and once I got it…well, it all fell into place - regardless of what numbers you pass in.

If I got it wrong, however…then I’m back to square one

I think I’m missing something very basic. Been stuck on this problem for a few days (to be accurate - I can reverse-engineer the answer from the description but have no idea how it actually works. For the sake of making my lack of knowledge more obvious, I’ll use an array of [1,2,3,4,5]

The bit that’s confusing me (and I realize has been explained is

return multiply(arr, n - 1) * arr[n - 1];

The recursive bit is return multiply (arr, n-1). That ticks down until n=0. I can follow so far. We start at -1 because we’re checking an array – and because arrays start at 0, we start at -1 in the first pip as n would be one greater than the array length.

Arr[n-1] refers to the item in an array position of n-1 or one earlier.

In my head – what happens is first we go to array item 5, then multiply it by item 5. I realize what we are actually doing is multiplying item 5 by item 4. Is this because once we have (arr n -1), n is furthermore treated at -1 (or item 5 in the array), so arr[n-1] is essentially treating it at -2 (or item 4 in the array)? The point where I get lost is exactly when the value of n drops (I think I can reason it happens in the () brackets but have no idea why.

Please help

yes, it’s correct! :slight_smile:

I really can’t follow your explanation, I can’t help. take a look at the previous posts in this discussion maybe

Basically I’m not sure when the -1 applies to the function. When we get to [n-1] I think that because we already ran (n-1), [n-1] is essentially -2, as we already reduced by 1. I’ve gotten myself in spiderwebs over this as you can see.

when you have something in parenthesis, that is evaluated before being used like arr[4-1] first the operation is made, arr[3] and after that you get the element at index 3

same for function, sum(arr, 4-1) first the operation, then the function is called