Factorial using Recursion

 // program to find the factorial of a number
   function factorial(x){
 
   // if number is 0
   if(x == 0){
       return 1;
   }
   
   // if number is positive
   else{
   return (x * factorial(x - 1));
   }
   }
   
   const number = 4;
   
   // calling factorial() if num is non-negative
   if(number > 0){
        let result= factorial(number);
        alert(result);
   }

Comments

Popular posts from this blog

Average of All Indexes of Array

JavaScript Coding Challenge-8

Largest and Lowest Number of the Array using JavaScript