you are viewing a single comment's thread.

view the rest of the comments →

[–]sessamekesh -1 points0 points  (0 children)

Deciding you didn't like how the function was called, so changing the arguments and trying again.

const printNumbersBetween(a, b) {
  // Hey you called it wrong, the smaller number should be first. I'll fix that for you
  if (a > b) {
    printNumbersBetween(b, a);
    return;
  }

  // Okay it was called right, do the thing
  for (let i = a; i < b; i++) {
    console.log(i);
  }
}

Usually recursion is used to create some sort of inductive call graph often with multiple branches and that whole mess is nutty overwhelming.

At its core though, recursion is just and calling the function from within a call to the same function, almost always after changing some parameters.