Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
52 lines (42 loc) · 1.03 KB

File metadata and controls

52 lines (42 loc) · 1.03 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 ]
*/
Morty Proxy This is a proxified and sanitized view of the page, visit original site.