How do I think about recursion.

I got the idea for problems like factorials or finding sum from 1 to n, where there’s a pattern visible. Like this:

Source:https://99x.io/blog/recursion-is-not-hard-here-is-the-right-way-to-think

But I’m confused for things like addition. I know the answer but I am unable to figure out how we came to the answer. Could you guys help me on this?

Can you give an example of what you mean for addition? Are you trying to add two numbers using a recursive approach?

I got for addition of two numbers. Can you describe one for division of two numbers a/b?

For addition, it’s a+b=a-1+1+b
=(a-1)+(b+1)
=Sum(a-1,b+1).
Now I need for division

I got for division as well. It’s
(a-b+b)/b
= (a-b)/b + b/b
=1+(a-b)/b/

Hey mate,
There are multiple way to call functions repeatedly.

You can for example save the result of a function call to a variable and then call it again e.g.

// call the add function with the previous variable until the last run equals 10
function add (z){ return z + 1 }

let run1 = add(1)
let run2 = add(run1)
let run3 = add(run2)
let run4 = add(run3)
let run5 = add(run4)
let run6 = add(run5)
let run7 = add(run6)
let run8 = add(run7)
let run9 = add(run8)

Or call it in a loop e.g.

function add (z){ return z + 1 }

// while "a" is less 10 make "a" equal to add "a" BUT stop at 10
let a = 1;
while( a < 10 ){ a = add(a) }

Or you have a function call itself from inside itself (recursion) e.g.

// if x equals 10 return x else call recursion with the value of x again
function recursion (x){
  if(x == 10) return x
  else return recursion(x + 1)
}

recursion(1)

The main thing that should concern you is having a break case so you don’t get excessive stacks or an endless loop…