-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorialize_a_number.js
More file actions
53 lines (41 loc) · 1.23 KB
/
factorialize_a_number.js
File metadata and controls
53 lines (41 loc) · 1.23 KB
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
// Factorialize a Number
// Return the factorial of the provided integer.
//
// If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
//
// Factorials are often represented with the shorthand notation n!
//
// For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
// factorialize(5) should return a number.
// factorialize(5) should return 120.
// factorialize(10) should return 3628800.
// factorialize(20) should return 2432902008176640000.
// factorialize(0) should return 1.
function factorialize(num) {
var arr = [];
// 0! always equals 1
if (num === 0) {
return 1;
} else {
// start loop to create array of numbers
for (i = num; i > 0; i--) {
arr.unshift(i);
}
// reduce array by multiplying each number by the next and return total
var fact = arr.reduce(function(a, b) {
return a * b;
});
return fact;
}
}
factorialize(0);
//_____________________________________________
//Spoiler Alert!
//Solution ahead!
//Best Code Solution using Recursion:
// function factorialize(num) {
// if (num === 0) { return 1; }
// return num * factorialize(num-1);
// }
// factorialize(5);
//_____________________________________________