Quick actions

cmd+k|ctrl+k

Navigation

Languages

Finding Factorial of Number using Recursive and Iterative Functions

Snippet info

Language

JavaScript

Visibility

public

Author

dhamankovachi1

Created

2023-11-28T11:40:11.104592Z

Updated

2023-11-28T11:40:11.104592Z

// Write two functions that finds the factorial of any number. One should use recursive, the other should just use a for loop
let answer=1;
function findFactorialRecursive(number) {
  //code here
  if(number===2){
      return 2;
  }
  else if(number===0 || number===1){
      return 1;
  }
  return number * findFactorialRecursive(number-1);
  
  
}

function findFactorialIterative(number) {
  //code here
  if(number===0 && number===1){
      return 1;
  }
  for(let i=number;i>0;i--){
      answer=answer*i;
  }
  return answer;
}

findFactorialIterative(0)
findFactorialRecursive(4)
INFO