Factorial

Run Settings
LanguageJavaScript
Language Version
Run Command
// Write two functions that finds the factorial of any number. One should use recursive, the other should just use a for loop function findFactorialRecursive(number) { if (number === 1 || number === 0) { return 1; } console.log(number - 1, "*", number - 2); return (number) * findFactorialRecursive(number - 1); } function findFactorialIterative(number) { let ans = 1; for (let i = 1; i <= 5; i++) { ans *= i; } return ans; } console.log(findFactorialRecursive(5));
// Write two functions that finds the factorial of any number. One should use recursive, the other should just use a for loop function findFactorialRecursive(number) { // O(n) if (number === 2) { return 2; } return number * findFactorialRecursive(number - 1); } function findFactorialIterative(number) { // O(n) let ans = 1; if (number === 2) { ans = 2; } for (let i = 2; i <= number; i++) { ans *= i; } return ans; } console.log(findFactorialRecursive(5), findFactorialIterative(5));
Editor Settings
Theme
Key bindings
Full width
Lines