Code Interview – Coderbyte – First Factorial – Code challenge – JavaScript Solution Source Code

First Factorial

Coderbyte

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

function FirstFactorial(num) { 

if (num === 0 || num === 1) { // Base case: the factorial of 0 and 1 is 1
    return 1;
} else { // Recursive case: multiply num by the factorial of num - 1
    return num * FirstFactorial(num - 1);
  }

}
   
// keep this function call here 
console.log(FirstFactorial(readline()));

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *