Skip to content

Instantly share code, notes, and snippets.

@texora
Last active June 19, 2020 05:17
Show Gist options
  • Save texora/85b0d76cca4a2fbb1aafea95cfa9265c to your computer and use it in GitHub Desktop.
Save texora/85b0d76cca4a2fbb1aafea95cfa9265c to your computer and use it in GitHub Desktop.
function power(num, pow) {
return pow !== 0 ? num * power(num, pow - 1) : 1;
}
function factorial(num) {
return num !== 0 ? num * factorial(num - 1) : 1;
}
// test
factorial(5)
5 * factorial(4)
4 * factorial (3)
3 * factorial(2)
2 * factorial(1)
1 * factorial(0)
1
1
1 * 1
2 * 1 * 1
3 * 2 * 1 * 1
4 * 3 * 2 * 1 * 1
5 * 4 * 3 * 2 * 1 * 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment