Last active
February 23, 2022 15:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================ | |
# 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