Recursion, answer please!

Hello, i have watched videos, and haven’t been able to formulate the answer. Can some please give me a spoiler, thank you in advance

  **Your code so far**
function sum(arr, n) {
// Only change code below this line

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

} else {
  return sum(arr, n - 1) + arr[n - 1];
}
}
console.log(num)
num--;
countDown(num);
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0

Challenge: Replace Loops using Recursion

Link to the challenge:

I see first syntax issues

For example, you have defined the function twice, that will not work

And you have a variable num and a function countDown that you have never defined

It seems you tried to copy solutions of other recursion challenges, copy pasting without understanding it’s not going to help you

i have read and watched the help video section several times, this is what i have come up with now. Im asking for help because I dont understand it.

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

if(n <= 1) {
return 0;

} else {
return sum(arr, n - 1) * arr[n - 1];
}
}

// Only change code above this line

I will share my approach to recursion.
It works for me when it comes to simple cases.

Example
Task: make recursive function which returns product of
2  positive numbers a and b

Hint: for the sake of learning use basic math rule:
a * b >>>> it is the same as :  a + a + ...+a    b times

Thought process:
recursion consists of 2 main things:

base case - it’s usually MOST SIMPLE case of your problem,
for us it will be case when b===1 (not 0, we are doing this stuff for positive numbers for now)
without BASE CASE our recursion will be the same mess as infinite loop:
it will never end

recursive step is the second thing we need
and here is often the most confusion begins

How I am trying to think:

I grab some case of my problem.
Not the base case , but something what can be easily analyzed

For this example I will grab case when b===4.
Lets say a === 2 (we can use any a in our case
not really that big of a deal for us in our studies now)

By the way, my fucntion is called
recursivePositivesProduct

recursivePositivesProduct(2,4) must return 8

also I can say
recursivePositivesProduct(2,b) must return 8 when b ===4

And now grab little more simple case, adjacent to our case above,
for us it will be (b - 1)=== 3

recursivePositivesProduct(2,3) must return 6

also I can say
recursivePositivesProduct(2,b - 1) must return 6 when b ===4

So, KEY THOUGHT:
Recursive step is the difference between adjacent cases, for us it is the difference
between

recursivePositivesProduct(2,b) =>>> 8
and
recursivePositivesProduct(2,b - 1) =>>>6

so for our little task this difference easy to grasp:
for a === 2 =>>> difference is 2
in general difference is equal to the value of a
And now when I know the difference I can say:

recursivePositivesProduct(a,b)
it is the same as
recursivePositivesProduct(a,b - 1) + a

And now we basically need to add base case and recursive step together

const recursivePositivesProduct = (a,b) => {
//base case
  if (b === 1) {
    return a; ///a * 1 === a obviously
  }
//recursive step
  else {return recursivePositivesProduct(a, b - 1) + a}
}

console.log(recursivePositivesProduct(7, 1))//7
console.log(recursivePositivesProduct(4,5))//20
console.log(recursivePositivesProduct(9,5))//45

The final goal is summing all numbers, so why there is a multiplication here?

I really appreciate your response and time taken out to explain to me.
I have been humbled by this excersise. Can you please show the step by step on this exact question? I have been stuck on this for a week now. This is what i got from what i have read.

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

if(n <=1) {
return 0;

} else {
return sum(arr, n - 1) + arr
}

}

// Only change code above this line

About your code, you have this

where sum(arr, n-1) returns a number and arr is an array, you can’t sum a number to an array - do you maybe want to sum one of the items inside arr?

this is a really comprehensive guide on recursion, it may be helpful to you:

I could, but it would be me solving your coding problem. It’s not the best way to help somebody with learning.
Guide provided in the post above is very thorough, try to dive in to it.

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