FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript-Interview-Question/Fibonacci.js at master · shaantanu9/JavaScript-Interview-Question · GitHub
shaantanu9
/
JavaScript-Interview-Question
Public
forked from
namitmalasi/JavaScript-Interview-Question
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript-Interview-Question
/
Fibonacci.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (27 loc) · 990 Bytes
Breadcrumbs
JavaScript-Interview-Question
/
Fibonacci.js
Copy path
File metadata and controls
35 lines (27 loc) · 990 Bytes
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
// The Fibonacci sequence, also known as Fibonacci numbers, is defined as the sequence of numbers in
// which each number in the sequence is equal to the sum of two numbers before it. The Fibonacci
// Sequence is given as:
// Fibonacci Sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21, ….
// Approach 1(For loop)
function
fibonacciSeriesWithForLoop
(
length
)
{
let
fibonacciSeries
=
[
0
,
1
]
;
for
(
let
i
=
2
;
i
<=
length
;
i
++
)
{
fibonacciSeries
[
i
]
=
fibonacciSeries
[
i
-
1
]
+
fibonacciSeries
[
i
-
2
]
;
}
return
fibonacciSeries
;
}
console
.
log
(
fibonacciSeriesWithForLoop
(
8
)
)
;
// Approach 2(Recursion)
function
fibonacciSeriesWithRecursion
(
length
)
{
if
(
length
==
0
)
{
return
[
0
]
;
}
else
if
(
length
==
1
)
{
return
[
0
,
1
]
;
}
else
{
let
fiboSeries
=
fibonacciSeriesWithRecursion
(
length
-
1
)
;
let
nextElement
=
fiboSeries
[
length
-
1
]
+
fiboSeries
[
length
-
2
]
;
fiboSeries
.
push
(
nextElement
)
;
return
fiboSeries
;
}
}
console
.
log
(
fibonacciSeriesWithRecursion
(
8
)
)
;
Back
|
FazBrowse Home
|
New Git URL