Skip to content

Instantly share code, notes, and snippets.

@arnonate
Created September 18, 2020 19:25
Show Gist options
  • Save arnonate/282392c40569fef6bda599e32d62b802 to your computer and use it in GitHub Desktop.
Save arnonate/282392c40569fef6bda599e32d62b802 to your computer and use it in GitHub Desktop.
// 1. ID base case
// 2. ID recursive case
// 3. Return where appropriate
// 4. Write procedures that bring you closer to base case
// Simple factorial example with Recursion
const factorial = n => {
return n < 2 ? 1 : n * factorial(n - 1);
};
console.log(factorial(1));
console.log(factorial(2));
console.log(factorial(3));
console.log(factorial(4));
console.log(factorial(5));
// Walking a tree
function walkTree(node) {
if (node == null) return;
for (var i = 0; i < node.childNodes.length; i++) {
walkTree(node.childNodes[i]);
}
}
// Recursion sets up a FUNCTION STACK
function foo(i) {
if (i < 0) return;
console.log("begin: " + i);
foo(i - 1);
console.log("end: " + i);
}
foo(3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment