Algorithm

[coderbyte/js] First Factorial

죠죠_ 2022. 7. 7. 12:55

문제 : 

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) { 
  let sum = 1;
  for (i = num; i > 0; i--) {
    sum = sum * i;
  }
  return sum; 

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

쉽다고 생각하고 제출했는데 모법답안 보고 이렇게 짰어야 했다.. 싶고 

재귀함수 쓰는게 아직 어색하다고 해야하나 

사실 제대로 못 쓰고 있는 것같다

function FirstFactorial(num) { 
    return (num === 1 ? 1 : num * FirstFactorial(num - 1));
}