Loop replacement with recursion

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 code above this line
}

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Replace Loops using Recursion

Link to the challenge:

What does this mean? I don’t think it is what you want.

ok.what is the way forward?

Well, first, do you know what the piece of code I highlighted does? What is [n - 1]

returns from the first n element right??

I’m talking about just the [n - 1]. There is no return there. n - 1 is just a number inside of a set of brackets. What does a number inside of a set of brackets mean?

it means an array right??

Yup. You made an array there. But sum(arr, n - 1) is a number. You can’t add an array and a number.

You can add a number and a number though. If you have the sum of the first 5 elements of an array, what should you add to that sum to get the sum of the first 6 elements of the array?

Since it is an array, that line of code should be:
Mod Edit: SOLUTION REDACTED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

1 Like

1 are my correct???

Your code isn’t correct, no. You are adding together a number and an array. That doesn’t work, like I said above.

Oh… I am new here… Thanks for the correction

the value of the first 5 right??

Since you are adding the elements of the array, it should be arr[n - 1] and not [n - 1] to access the elements and then add it.
Hope it helps !!!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.