Skip to content

Instantly share code, notes, and snippets.

@codistwa
Last active February 23, 2022 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codistwa/79cc060d747328c380073ee1154b2a4a to your computer and use it in GitHub Desktop.
Save codistwa/79cc060d747328c380073ee1154b2a4a to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/recursion. More courses on https://codistwa.com
// ============================================================
// Recursion with one parameter
// ============================================================
const factorial = (num) => {
if (num === 1) {
return 1;
}
return num * factorial(num - 1);
}
console.log(factorial(5)); // 120
// ============================================================
// Recursion with two parameters
// ============================================================
const power = (base, exponent) => {
if (exponent === 0) {
return 1
}
return base * power(base, exponent - 1)
}
console.log(power(2, 3)); // 8
# ============================================================
# Recursion with one parameter
# ============================================================
def factorial(n):
if n == 1:
return n
else:
return n * factorial(n - 1)
print(factorial(5)) # 120
# ============================================================
# Recursion with two parameters
# ============================================================
def power(base, exponent):
if exponent == 0:
return 1
return base * power(base, exponent - 1)
print(power(2, 3)) # 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment