forked from aishraj/JavaScript-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path010.js
More file actions
41 lines (30 loc) · 707 Bytes
/
010.js
File metadata and controls
41 lines (30 loc) · 707 Bytes
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
/*
* <!--
* This program is distributed under
* the terms of the MIT license.
* Please see the LICENSE file for details.
* -->
*/
/*
* Write a recursive method to generate nth Fibonacci number.
* F(n) = F(n-1) + F(n-1); F(0) == 0, F(1) == 1;
*/
/*____________________________________________________________________________*/
function fibonacci(n) {
if (n === 0) {
return 0;
}
if (n === 1) {
return 1;
}
if (n < 1) {
return -1;
}
return fibonacci(n-1) + fibonacci(n-2);
}
/*____________________________________________________________________________*/
console.log( fibonacci(42) );
/*
Output: ($ /usr/bin/node 010.js)
267914296
*/