Code Programming Tutorials, Guides & Best Practices
Explore 184+ expertly crafted code tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Code
javascript
Calculating Factorial
// Recursive function to calculate factorial
function calculateFactorialRecursive(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * calculateFactorialRecursive(n - 1);
}
}
// Iterative function to calculate factorial
function calculateFactorialIterative(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Example usage
const numberToFactorialize = 5;
const recursiveResult = calculateFactorialRecursive(numberToFactorialize);
const iterativeResult = calculateFactorialIterative(numberToFactorialize);
console.log(`Factorial of ${numberToFactorialize} (Recursive):`, recursiveResult);
console.log(`Factorial of ${numberToFactorialize} (Iterative):`, iterativeResult);
Jan 26, 2024
Read More