Help with recursion

hi all!

I am trying to wrap my head around the recursion idea, i get that the function keeps calling itself but i can’t really understand how to sit down and write the code… any body have good examples of using it from a basic level to a bit more advanced? thanks for the help!

This is an example of recursion in a real world using mirrors.

ok nice, i see what it is and i can understand the i idea but im not sure i understand the code or how to implement it…

1 Like

An example. This code snippet sums numbers to n using recursion.

function sumTo(n) {
  if (n == 1) return 1;
  return n + sumTo(n - 1);

alert( sumTo(100) );

ok cool, so that if im not mistaken would give you an output that looks like

1 + 2 + 3 + 4… etc

its using recursion because its calling on itself here in this line?

can you give me an example of when using recursion would be useful in a project?
thanks for the help walking me through this

1 Like

Yes, You have grasped the core idea.

1 Like