Basic JavaScript - Replace Loops using Recursion

I am not understanding where I am going wrong

 function sum(arr, n) {
  // Only change code below this line
function sum(arr, n) {
 if (n <= 0) {
   return 0;
 } 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/ Safari/537.36 Edg/108.0.1462.54

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

It looks like you pasted the solution inside of your code?

Why are there two function definitions?

thanks for pointing the mistake I mistakenly wrote the functions twice.

1 Like

I strongly recommend against looking at anybody else’s solutions until your code works. This is the part of the curriculum where problems start being less about syntax and more about problem solving.

yeah I avoid but when i get stuck i used it

I really recommend talking to other people when you’re stuck instead of looking at other people’s solutions. There is always time to look up other solutions after you get a working code of your own!

thanks sir for the advice will keep it in mind!

1 Like

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