forked from aishraj/JavaScript-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path003.js
More file actions
52 lines (42 loc) · 1.03 KB
/
003.js
File metadata and controls
52 lines (42 loc) · 1.03 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
/*
* <!--
* This program is distributed under
* the terms of the MIT license.
* Please see the LICENSE file for details.
* -->
*/
/*
* Implement getAllFactorials method in 002.js *without* using recursion.
*/
/*____________________________________________________________________________*/
/**
* @function {public static} getAllFactorials
*
* Gets all the factorials before n.
*
* @param {Integer} n - the number to get the factorials of.
*
* @return an `Array` containing all the factorials before n.
*/
function getAllFactorials(n) {
var stack = [];
var val = 1;
var i = 0;
var temp = 0;
if (n <= 1) {
stack.push(1);
return stack;
}
for(i = 1; i <= n; i++) {
temp = val * i;
stack.push(temp);
val = temp;
}
return stack.reverse();
}
console.log(getAllFactorials(10));
/*____________________________________________________________________________*/
/*
Output: ($ /usr/bin/node 003.js)
[ 3628800, 362880, 40320, 5040, 720, 120, 24, 6, 2, 1 ]
*/