Recursion Factorial

Run Settings
LanguageJavaScript
Language Version
Run Command
//recurrive function /* 1. Identify the base case 2. Identify the recursive case 3. Get closer and closer and return when needed. Usually you have 2 returns */ let counter = 0; function inception(){ if(counter > 3){ return 'done!'; } counter++; return inception(); } //console.log(inception()); //Write two functions that finds the factorial of any number. One should use // recurrive. One should use recursive, the other should just // Factorial 5! = 5 * 4 * 3* 2 * 1; function FactorialRecursive(number){ if(number === 2) { return 2; } return number * FactorialRecursive(number - 1) } console.log(FactorialRecursive(5)); function FactorialIterative(number){ let answer = 1; if(number === 2){ return 2; } for(let i = 2; i <= number; i++){ answer = answer * i ; } return answer; } console.log(FactorialIterative(5))
Editor Settings
Theme
Key bindings
Full width
Lines