Replace loop using recursion

Tell us what’s happening:
I think the code is buggy, since i am using recursion and it is still asking me to use recursoin in order to get the lesson.
by the way thanks to the creators of this site for including extra lessons.

Your code so far

js

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Replace Loops using Recursion

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/replace-loops-using-recursion

1 Like

It doesn’t like that the function call is inside of an if statement. Move that function call to the end of the function body, and things should be better.

like this?

function sum(arr, n) {
  // Only change code below this line
  if (n <= 0) {
    return arr[0];
  } 
    return sum(arr, n - 1) + arr[n];
  // Only change code above this line
}
1 Like

it still doesnt log it as recursion, i think it might be jsut the update?
or because it is a new lesson.

the code passes for me
can you try reloading the page? clearing cache?

I’ve just checked, and here’s how it’s testing if you’re using recursion:

{
  text: "You should use recursion to solve this problem.",
  testString: "assert(removeJSComments(sum.toString()).match(
    /sum\(.*\).*\{.*sum\(.*\).*\}/s)
  );"
}

So it doesn’t matter if the call to the sum function is in an if statement or not. You just need to call that sum function somewhere in the function for that test to pass.

Your code is good. I recommend following @ ieahleen’s advice.

thanks guys ill clear the cache and give it another go.

maybe its something with my firefox, since I erased everything, history, cahce, cookies and this code still doesnt work…

try google chrome maybe?

code passes for me too

hey guys, I believe the problem is the maintenance on the site. I cant sign in on chrome, so i think i just have to wait to keep coding. Thanks for all the help

I am also not able to log in. But the challenges should work even without log in, as declared (and it is working for me). So that cannot be a problem, i think.

I am not sure why it wont work in firefox, but it worked perfectly on Google Chrome. we might have to use google chrome for freecodecamp from now on?

Its works only with Chrome. I tested it with Firefox and Edge and the task do not to pass

1 Like

This does work in Firefox now, as I just tested this.

[Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:70.0) Gecko/20100101 Firefox/70.0]

1 Like


It’s little bit changed…

Good day, It’s not working!

when n is 3 you need to sum the items at index 0, 1, 2 not also that at index 3
it is small changes, but you can do it!

As Itsari mentioned, the challenge has changed a bit.

I’ve been trying to understand this for a couple days now with no luck. I decided to look for the solution code and try to understand it that way.

Can someone help me understand how this works, maybe with reference to what happens with the call stack?

I also had this problem today. The correct answer was only accepted when I cleared cache in Chrome.
Solution: Przechwytywanie

4 Likes