My solutiion makes sense to me but I have n clue why this does not work

Tell us what’s happening:
Describe your issue in detail here.

``````   **Your code so far**
``````
``````
function sum(arr, n) {
// Only change code below this line
if(n < 0){
return 1;
}else {
return sum(arr, n - 1) + arr[n-1];
}

// Only change code above this line
}
``````
``````   **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36`.

Challenge: Replace Loops using Recursion

Why is your sum starting with 1?

I just assumed because the example does this I may as well stick to the same method of writing the code for continuity, also from my understanding the case is just used to stop the recursion from looping infinitely so I thought the base case I wrote was just if I get a value that is less than 0 then the function should just return 1, was that the wrong thing to do ?

Well, try experimenting with your code:

``````function sum(arr, n) {
// Only change code below this line
console.log("n:" + n);
console.log("arr: [" + arr + "]");
if (n < 0) {
return 1;
} else {
return sum(arr, n - 1) + arr[n - 1];
}
// Only change code above this line
}

console.log(sum([2, 2], 0)); // Should be 0
``````

What do you see?

Before I can easily explain the issue with `return 1`, we have another problem to find.

should the if statement be less than or equal to zero ?
ps do the initial console. Logs help with anything apart from visualizing what’s going on with the code?

Well, try `if (n <= 0)` and see how the result changes. That will help (the real question though, is why)

yes, the console logs are just there to help us see what’s happening inside the recursive function.

so I implemented console.log ( console.log(sum([1,2],2)) ) into the code and called the function, but I got 4 so I think for some reason my function is adding the 1 index to its self, which is weird because it should be doing the same thing as the example code so now I’m actually confused , I get that recursion in its simplest form is a function calling itself and I thought my solution made some sort of sense, also my solution is the same as the example code however its addition instead of multiplication

also as to why less than or equal to zero, I assume its because we have a possibility of getting 0 as an n value so I wanted to account for that possibility but dismiss any values below zero.

Well, your function says "`sum` the first `n` elements of `arr`".

This case

effectively means that the "`sum` of the first `0` elements of `arr` is `1`".

should it be
if (n< 0)
return 1 ;
?

How would changing `if (n < 0)` help fix that in your code

``````sum([2, 3, 4], 0) // i.e. add nothing together
``````

means that the " `sum` of the first `0` elements of `[2, 3, 4]` is `1` "?

I don’t understand how either the example code or my code works, though I did,I thought that was the right thing to do as it allows values of zero to be used as n and values less than zero to be the stopping criteria, don’t know how the problem works

You are making a function that returns the sum of `n` elements in the array.

This means that your return value must be a sum.

This literally says that the sum of 0 elements (which means, add nothing together) is 1. Does that make sense?

If you add nothing together, how much do you have?

yes it does

if there are 0 elements to be summoned it should equal zero, I understand that part.

But your code says that the sum of 0 elements is 1.

That’s what it means when you `return 1` for `sum` when `n <= 0`.

I understand that its wrong now.

Cool. So what should you updated code be?

genuinely not sure I’m going to go and try and fix it ill message back if my head starts hurting again might need to write down some things

right so went to have food and changed the condition and it worked, genuinely was insanely simple, thank you very much for the guidance. Also understood why I couldn’t use the same base case for multiply as i did for the sum.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.