Quick actions

cmd+k|ctrl+k

Navigation

Languages

Factorial Recursive vs Iterative in JavaScript

Snippet info

Language

JavaScript

Visibility

public

Author

patrykstachowiak

Created

2025-03-08T21:40:40.315115Z

Updated

2025-03-08T21:41:39.482879Z

// 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 === 2) return 2;
    let answer = number * findFactorialRecursive(number-1);
  return answer;
}

function findFactorialIterative(number) {
  let answer = 1;
  for (let i=2; i<number+1;i++)
  {
      answer *= i;
  }
  return answer;
}

console.log(findFactorialRecursive(5));
console.log(findFactorialIterative(5));
INFO