Basic JavaScript - Replace Loops using Recursion

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

Your code so far

How do I figure out the equation. Run code test is stating that not using Your code should not rely on any kind of loops (for or while or higher order functions such as forEach , map , filter , or reduce .) and use a recursion. I have theses two steps. I keep attempting to plug in numbers but haven’t figure out the equation.

function sum(arr, n) {
  // Only change code below this line

if (n <= 10) {
  return sum;
} else {
 return sum(arr.length) * arr[n/5];

}

  // 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/116.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

One important part of doing a recursion is to call the same function within the function. This means all parameters should be set. The definition of the function sum has two parameters: arr and n.
When looking your call of the function sum I see you only give one parameter:

sum(arr.length)

Do you mean something like this?

function sum(arr, n) {
// Only change code below this line

if (n <= 0) {
return sum + n;
} else {
return sum(arr.length) * arr[n/5];

}

// Only change code above this line
}

To call a function within itself, literally, means to call a function within itself. So at a bare minimum, a recursive function is like this:

function sum(arr, n) {
  ...
  sum(arr, n)
}

So if a function keeps calling itself, then when does it stop calling itself? (you may ask).
Ans: We set a condition that a function must stop calling itself when that condition is met. For example, when there is no more element in the array to return:

function sum(arr, n) {
  if (n == 0) {  
    return 0;
  }

  sum(arr, n)    
}

But how does n get to 0 if it starts at 2 for example, you may ask.
Ans: You reduce it all each call until the base condition is met:

function sum(arr, n) {
  if (n == 0) {  
    return 0;
  }

  sum(arr, n - 1)    // Each time sum() calls itself, n is 3, then 2 then 1 (if n is initially 3).
                     // n eventually reaches 0 to meet the condition above.
}

We are still not done though. How do you go about summing the first n items in the array? You now have the framework, so I will leave that to you.

3 Likes

function sum(arr, n) {
// Only change code below this line
if (n == 0) {
return 0;
}
else {
if (n > 0) {
return sum(arr.length, n - 1);
}

}

// Only change code above this line
}

You are close, but not quite there yet. Remember, you are calling the same function within itself, so the function should accept the same arguments each time it calls itself,

return sum(arr.length, n - 1);

Take a look at this line. Here you are calling sum(arr, n) again, but instead of calling it with an arr, and a number, you are calling it with two numbers (array.length returns the number of items in an array).

So that line should look something like :

function sum(arr, n) {
// Only change code below this line
  if (n == 0) {
    return 0;
  }
  else {
    if (n > 0) {
      return sum(arr, n - 1); //**
    }
  }

// Only change code above this line
}

But it is still not done. Currently, nothing is being done with the return value of each sum() call.

Take a close look at the example of a recursive function from the challenge page. If you have passed the test and are still not clear on what is happening, then don’t hesitate to ask again here.

There are also plenty of resources out there that you could refer to, for example:

1 Like

Thank you. I solved it and will take a look at the link you provided.

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