알고리즘 35 - Factorial

박진현·2021년 7월 19일
0

Q.

Description:
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 4 3 2 1 = 120. By convention the value of 0! is 1.

Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw an exception of type ArgumentOutOfRangeException (C#) or IllegalArgumentException (Java) or RangeException (PHP) or throw a RangeError (JavaScript) or ValueError (Python) or return -1 (C).

More details about factorial can be found here.

A)

function factorial(n)
{
  if (n < 0 || n > 12) throw new RangeError()
  if (n <= 1) return 1;
  return n * factorial(n - 1);
}

throw === 사용자 정의 예외 던지는 것
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/throw

profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글