-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-11-22 Codewars 7 Kyu Fundamentals - Factorial.js
60 lines (42 loc) · 1.6 KB
/
2023-11-22 Codewars 7 Kyu Fundamentals - Factorial.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 11/22/23 Wednesday Codewars 7 Kyu Fundamentals - Factorial
// https://www.codewars.com/kata/54ff0d1f355cfd20e60001fc/train/javascript
/*
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(https://www.wikiwand.com/en/Factorial).
*/
// 2nd attempt
function factorial(n) {
if(n < 0 || n > 12) {
throw new RangeError('Number out of range'); // Had to set up RangeError with throw
} else {
let numbers = [];
for(let i = n; i > 0; i--) {
numbers.push(i);
}
return numbers
.reduce( (acc, cur) => acc * cur, 1);
}
}
// 1st attempt
function factorial(n) {
if(n < 0 || n > 12) {
return RangeError;
} else {
let numbers = [];
for(let i = n; i > 0; i--) {
numbers.push(i);
}
return numbers
.reduce( (acc, cur) => acc * cur, 1);
}
}
/* =============
Other Solutions
============= */
function factorial(n) {
if (n < 0 || n > 12)
throw new RangeError();
return n <= 1 ? 1 : n * factorial(n - 1);
}
// Factorial is "n-1" instead of just "n" because just "n" would lead to infinite recursion