dsa.js
Version:
Data Structures & Algorithms in JS
25 lines (20 loc) • 430 B
JavaScript
// tag::snippet[]
/**
* Get Fibonacci number on the n-th position.
* @param {integer} n position on the sequence
* @returns {integer} n-th number
*/
function getFibonacci(n) {
if (n < 0) return 0;
if (n < 2) return n;
let prev = 0;
let result = 1;
for (let i = 1; i < n; i++) {
const temp = result;
result += prev;
prev = temp;
}
return result;
}
// end::snippet[]
module.exports = getFibonacci;