FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Dynamic-Programming/FibonacciNumber.js at master · sobitd/JavaScript · GitHub
sobitd
/
JavaScript
Public
forked from
TheAlgorithms/JavaScript
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
/
Dynamic-Programming
/
FibonacciNumber.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (23 loc) · 698 Bytes
Breadcrumbs
JavaScript
/
Dynamic-Programming
/
FibonacciNumber.js
Copy path
File metadata and controls
27 lines (23 loc) · 698 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
/**
*
@function
fibonacci
*
@description
Fibonacci is the sum of previous two fibonacci numbers.
*
@param
{
Integer
} N - The input integer
*
@return
{
Integer
} fibonacci of N.
*
@see
[Fibonacci_Numbers](https://en.wikipedia.org/wiki/Fibonacci_number)
*/
const
fibonacci
=
(
N
)
=>
{
if
(
!
Number
.
isInteger
(
N
)
)
{
throw
new
TypeError
(
'Input should be integer'
)
}
// memoize the last two numbers
let
firstNumber
=
0
let
secondNumber
=
1
for
(
let
i
=
1
;
i
<
N
;
i
++
)
{
const
sumOfNumbers
=
firstNumber
+
secondNumber
// update last two numbers
firstNumber
=
secondNumber
secondNumber
=
sumOfNumbers
}
return
N
?
secondNumber
:
firstNumber
}
export
{
fibonacci
}
Back
|
FazBrowse Home
|
New Git URL