Coderbyte(JavaScript) - First Factorial

미정·2021년 8월 20일
0

Coderbyte | JavsScript

목록 보기
2/4

✅문제

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 result = 1;
  while(0 < num){
    result *= num--;
  }
  return result; 
}
//console.log(FirstFactorial(readline())); 
FirstFactorial(readline());

이미 팩토리얼에 대한 문제를 알고 있어서 큰 문제 없이 바로 풀었다.
시간측정 테스트 점수가 5점 만점에서 2점을 맞아, 이상하다 싶어서 속도 면에서 문제가 될만하다고 생각한 console.log()를 없앴더니 5점 만점을 받았다.


🎈다른 사람의 풀이

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

삼항연산자를 이용해서 깔끔하게 재귀함수로 한 것이 너무 맘에 든다. 굿굿! 이젠 재귀함수 조금 친해져서 두렵지는 않다~!



참고 자료 및 사이트 (감사합니다)

profile
공사중

0개의 댓글