Basic JavaScript: <Replace Loops using Recursion>

Tell us what’s happening:

Your code so far

i dont get please assist, i dont know what am doing wrong the code looks good to me…

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/83.0.4103.116 Safari/537.36.

Challenge: Replace Loops using Recursion

Link to the challenge: text

Without knowing what you don’t get, as far as recursion goes, it’s challenging to help. Is there something specific that’s confusing you?

Another option, when i was trying to explain this very topic a while back, i wrote a blog post about it. It doesn’t deal with the sum question, i wrote it as an exponent thing (so 3 to the power of 7, for example), but the process is the same.

First, of all, you’ve got two function sum(arr, n) { lines here.

1 Like