Basic JavaScript - Replace Loops using Recursion

Tell us what’s happening:

I am facing 2 problem here,

first is base case, I m trying to find if array length <= it length, I think my logic is wrong (can someone guide me how to be good at logic, I am at end of basic algo and i suck)
second problem is I m trying to increment the array but it second and 3rd iteration, I am getting 2 e.g.

1 arr
2 arr
2 arr

My expectation without the base case would be that i keep on running and increment until call stack is overflow.

What am i doing wrong?

Your code so far

function sum(arr, n) {
  // Only change code below this line
  if(arr.length <= arr.length) return
    let index = 0;
    console.log(arr[index++], "arr");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Basic JavaScript - Replace Loops using Recursion

just a random thought that I can make base case if(arr.length <= 0) return again It is just out of thin air, how can I make logical thinking decision?

Your function must always return a number. So any return statement must return a number. So that’s the first thing you need to fix.

Also, I think you are taking the wrong approach here basing decisions on the length of the array. Look at the example, does the length of the array change there? What is actually changing each time multiply is called. Use that as an example of how to create the sum function.

Only when you completely understand how the example code is working will you be able to create the code for the sum function.

I m console.log to check my answer each time

n is being changed, but here how could I know if in sum the n need to be <=0 ? is it safe to say that n changes everytime in the recrusion?

The questions you are asking lead me to believe that you do not understand how the example function (multiply) works. Again, you need to completely understand how that function works before you pass this challenge.

1 Like

To understand how multiply works, try walking through an example with real numbers. If I gave you the following:

multiply([7,8], 0);

Tell me both what that function call would return and why.

It will return base case because n = 0 and thus return 1

Perfect. Because n is 0 then the function will execute the return statement in the if block, which returns 1.

Now do the same for

multiply([7,8], 1);

Don’t just tell me the answer, but explain how the function created the answer. You can do this by replacing the variables in the code with their actual values. In other words, replace the variables in the return statement in the else block with their actual values.

I have drawn it out, as I am visual kind of person, in explaination, when multiply([7,8], 1); is called
It doesn’t meet the base case so go next line and call itself with [7,8] and n - 1 which is 1, so 1- 1 is 0.
Next it sees arr[n - 1] which is again 1-1 = 0, so arr[0] which is 7

Now I know it will open a execution of the function but now I am confused about how does

 multiply([7,8], 0) * 7

work because of *7. if I am making sense.

You are correct, this is what the return statement will be. Remember, the multiply function always returns a number, so you know that you will be multiplying the number returned by multiply([7,8], 0) by the number 7. Now look above, you already solved multiply([7,8], 0). It’s the first thing I asked you to explain. So what number does multiply([7,8], 0) return? And if you multiply that number by 7 then what does multiply([7,8], 1) return?

it returned 1 as base case was returning 1, right?

7*1 = 7?
but why we are multiplying 7 with 1, coz both r different as 1 was retruned when n was 0, and 7 was returned when n was 1, right?

the confusion is how can I mutiply

sorry but ur last post made it more confusing, I have updated my post with diagram.

Let’s look at the return statement again:

return multiply(arr, n - 1) * arr[n - 1];

You correctly replaced the variables in this statement with their actual values:

return multiply([7,8], 0) * 7;

And we already know that multiply([7,8], 0) returns 1, so we can replace that function call with its value:

return 1 * 7;

Which means the that multiply([7,8], 1) returns 7.

I’m not quite sure how you are confused by * 7? Remember, the original return statement is:

return multiply(arr, n - 1) * arr[n - 1];

And when we call the function as multiply([7,8], 1) then that return statment becomes:

return multiply([7,8], 0) * 7;

It seems like you get this part, so why would you be confused that you have to multiply the result of multiply([7,8], 0) by 7? That is clearly what the return is doing above, don’t you think?

How? return will still be arr which is [7,8] and oprand * with number 7 so it will be multiply([7,8], 0) * 7

Yes, and what value does multiply([7,8], 0) return?

If I told you there was a function called getRandomNumber() and it always returned a random number between 1 and 100, and I asked you what the following returned:

return getRandomNumber() * 7;

Could you explain to me how that return statement works? If so, please do.

It will return any number between 1 and 99 which then will be multiplied by 7, return exit the function but when it is in recursion it call it again with new params. did I say it right?

Exactly. The return statement has to wait for getRandomNumber() to return a value, then it multiplies that value by 7 and returns the resulting value from the function. So why can’t the following work the same way?

return multiply([7,8], 0) * 7;

Don’t let the fact that you are using recursion to solve this problem fool you into thinking that the function will behave differently from any other function. Recursion is a method of solving a problem, but it doesn’t change how functions work. Calling a function recursively does not change its behavior or make it special in some way. It is a separate, independent function call that returns a value, just like calling getRandomNumber() returns a value. So when you have:

return multiply([7,8], 0) * 7;

The return statement just needs to get the value returned by multiply([7,8], 0) and then multiply it by 7 and return the resulting value. You already know that multiply([7,8], 0) returns 1 because that is the base case. So

return multiply([7,8], 0) * 7;

Is the same thing as:

return 1 * 7;

Is this starting to make a little more sense?

1 Like

My latest code, but now I do not understand why n in parameter is greyed out whereas I m assigning n = 0