@maxinimize/

Recursion-factorial

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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) {
    return 1
  }
  number--
  return (number + 1) * findFactorialRecursive(number);

}

function findFactorialIterative(number) {
  let answer = 1
  for (let i = number; i > 0; i--) {
    answer = answer * i
  }
  return answer;
}

findFactorialIterative(5) 
findFactorialRecursive(5)
Native Browser JavaScript