File tree 3 files changed +64
-0
lines changed
L-B/0006 NthNumberFibonacci ( L-B )
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ const NthNumberFibonacci = n => {
2
+ if ( n <= 0 ) return 0
3
+ if ( n <= 1 ) return 1
4
+
5
+ return NthNumberFibonacci ( n - 2 ) + NthNumberFibonacci ( n - 1 )
6
+ }
7
+
8
+ module . exports = NthNumberFibonacci
Original file line number Diff line number Diff line change
1
+ # 0006 NthNumberFibonacci ( L-B )
2
+
3
+ ## Problem
4
+
5
+ Create a function that return the nth fibonacci number.
6
+
7
+ Fibonacci starts with ` 0 ` and ` 1 `
8
+
9
+ Fibonacci(n) = Fibonacci(n - 1) + Fibonacci(n - 2)
10
+
11
+ ## Test Cases
12
+
13
+ - NthNumberFibonacci(10) // 55
14
+ - NthNumberFibonacci(6) // 8
15
+ - NthNumberFibonacci(0) // 0
16
+ - NthNumberFibonacci(1) // 1
17
+ - NthNumberFibonacci(2) // 1
18
+ - NthNumberFibonacci(3) // 2
19
+
20
+ ## Solution
21
+
22
+ ``` javascript
23
+ const NthNumberFibonacci = n => {
24
+ if (n <= 0 ) return 0
25
+ if (n <= 1 ) return 1
26
+
27
+ return NthNumberFibonacci (n - 2 ) + NthNumberFibonacci (n - 1 )
28
+ }
29
+ ```
30
+
31
+ ## References
32
+
33
+ - [ Wikipedia] ( https://en.wikipedia.org/wiki/Fibonacci_number )
34
+ - [ Recursive MDN] ( https://developer.mozilla.org/en-US/docs/Glossary/Recursion )
35
+
36
+ ## Problem Added By
37
+
38
+ - [ GitHub] ( https://www.github.com/kennarddh )
39
+
40
+ ## Contributing
41
+
42
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
43
+
44
+ Please make sure to update tests as appropriate.
Original file line number Diff line number Diff line change
1
+ const { strict : assert } = require ( 'assert' )
2
+
3
+ const NthNumberFibonacci = require ( './NthNumberFibonacci.js' )
4
+
5
+ assert . equal ( NthNumberFibonacci ( 10 ) , 55 )
6
+ assert . equal ( NthNumberFibonacci ( 6 ) , 8 )
7
+ assert . equal ( NthNumberFibonacci ( 0 ) , 0 )
8
+ assert . equal ( NthNumberFibonacci ( 1 ) , 1 )
9
+ assert . equal ( NthNumberFibonacci ( 2 ) , 1 )
10
+ assert . equal ( NthNumberFibonacci ( 3 ) , 2 )
11
+
12
+ console . log ( 'All tests success' )
You can’t perform that action at this time.
0 commit comments