Skip to content

Commit

Permalink
Merge pull request #782 from Vijay-K-2003/master
Browse files Browse the repository at this point in the history
Add nthFibonacciNumber[javascript]
  • Loading branch information
vidit2512 authored Oct 4, 2021
2 parents b2599e1 + 33c5251 commit cb39de2
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions fibonacci_number/FibonacciNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function fibonacci(p) {
if(p === 1 || p === 2) {
return p-1; // base case
} else {

let a = 0;
let b = 1;
// 0 1 1 2 3 5 8 ...
let fib = 0;
for(var i = 2 ; i < p ; i = i + 1) {
fib = a + b;
a = b;
b = fib;
}

return fib;
}
}

function main() {
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});

readline.question("Enter value of n ", n => {
console.log(fibonacci(n));
readline.close()
});
}

main();


0 comments on commit cb39de2

Please sign in to comment.