FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Questions/recursion/003.js at master · devangnpatil/JavaScript-Interview-Questions · GitHub
devangnpatil
/
JavaScript-Interview-Questions
Public
forked from
aishraj/JavaScript-Interview-Questions
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Questions
/
recursion
/
003.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (42 loc) · 1.03 KB
Breadcrumbs
JavaScript-Interview-Questions
/
recursion
/
003.js
Copy path
File metadata and controls
52 lines (42 loc) · 1.03 KB
Raw
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 ]
*/
Back
|
FazBrowse Home
|
New Git URL