-
Notifications
You must be signed in to change notification settings - Fork 1
/
003-factorialize-a-number.js
61 lines (45 loc) · 1.06 KB
/
003-factorialize-a-number.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
61
/**
* Factorialize a Number.
*/
// 1st Solution
const factNumber = ( number ) => {
let result = 1;
for (let i = number ; i > 0; i-- ) {
result *= i;
}
return result;
};
console.log(factNumber(0));
console.log(factNumber(1));
console.log(factNumber(5));
console.log(factNumber(6));
console.log(factNumber(7));
console.log('-------------------------');
// 2nd Solution
const factNumber2 = ( number ) => {
if ( number == 0){
return 1;
}else{
return number * factNumber2(number - 1);
}
};
console.log(factNumber2(0));
console.log(factNumber2(1));
console.log(factNumber2(5));
console.log(factNumber2(6));
console.log(factNumber2(7));
console.log('-------------------------');
// 3rd Solution
const factNumber3 = ( number ) => {
let i;
for ( a = i; number >= 1; number--) {
i *=number;
}
return i;
};
console.log(factNumber3(0));
console.log(factNumber3(1));
console.log(factNumber3(5));
console.log(factNumber3(6));
console.log(factNumber3(7));
console.log('-------------------------');