Basic JavaScript - Replace Loops using Recursion

ok so if n is less then or equal to 0 return 1 if not return multiply arr n + 1 times arr n +1 how does this work where im suppose to end up with

  • sum([1], 0) should equal 0.

  • Failed:sum([2, 3, 4], 1) should equal 2.

  • Failed:sum([2, 3, 4, 5], 3) should equal 9.

does arr automatically equal 1 2 3 4 5?

I’m lost may take a miracle for me to understand this.

if n is 0 return n +1 that’s 1 right? then u multiply the other side of arr [ n +2] that would be 2? then 2 times 1 thats 2 or am I understanding this all wrong.

Your code so far

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

  // 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

Challenge: Basic JavaScript - Replace Loops using Recursion

Link to the challenge:

Hi @blkhertzz,

I’m not going to sugarcoat it. There’s quite a bit wrong with your function, as it stands. It’s relying on a multiply function that doesn’t exist. That multiply function is only talked about in the challenge to give you an example of how that recursion works.

However, since that multiply function has incredibly similar syntax to how you can program the sum function, that’s what I’m going to explain so that you can use the very same methodology to create a sum function.

Each time the multiply function is ran, it is basically adding to a stack of numbers that are going to do some operation. In the example given of the multiply function, it looks like this really:

(Number at n - 1) * (Number at n - 2) * ... until n == 0 then multiply by 1

The n value keeps being decremented by 1 every time you call it until n is less than or equal to 0. n will never be less than 0, though, unless you literally make the initial call to it something like multiply([1,2,3], -2).

So, if we take a real example, and say we have the numbers: 2, 3, and 4. And we want to multiply the first 2 together:

multiply([2,3,4], 2)

What ends up being returned is this:

3 * 2 * 1 //Which is evaluated to become the number 6

Why? Because, you called the function initially and said, “Hey, I want n = 2”. It then goes in and says “Okay, well the element there is actually at “n - 1” and that is the number 3 so let’s start to multiply that”. And at the same time, it calls itself again because it’s multiplying the next thing in the list, but this time, n = 1. Well, the number at that location is actually at n - 1, which is 0, and in this specific array is the number 2. It repeats this again putting the number 2 on to the multiplication and then says, “Ok, now n = 0”. Once that happens, the very first if statement executes, because n is now 0. It returns a 1 back to that initial list, which returns: 3 * 2 * 1 and that is evaluated to become 6.

This same exact concept can be directly applied to the sum function exchanging any multiplication for addition.

I hope that helps!

1 Like

Thank you so much for taking your time to explain this to me I really appreciate it.
Ok I understand I was using the wrong function here fixed that and I Now understand that N will always be less than 0 to keep the function going. Till I get the number I want.

return sum (arr, n - 1) + arr[n +2 ]; is this the same meaning as
-1 + 2 if n is <=0? would it not give me the result of
sum([1], 0) should equal 0.

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

Don’t modify the arr[n-1] part in the function. It looks like you replaced it with arr[n+2] which is not what you want. Remember, the recursive function is always decrementing so you should be calling n-1 in both places in the return statement.

Also, you should return a number, but it shouldn’t be 1. Think about this: in the multiply function, we have to return a 1 at the very end so that nothing is modified further. Same concept applies to the sum function. What number could be added to anything and not modify that set of numbers?

1 Like

ah thank you and your brain for helping me understand and figured it out.

1 Like

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