dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
31 lines (25 loc) • 567 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = fibonacciNth;
/**
* Calculate fibonacci number at specific position using Dynamic Programming approach.
*
* @param n
* @return {number}
*/
function fibonacciNth(n) {
let currentValue = 1;
let previousValue = 0;
if (n === 1) {
return 1;
}
let iterationsCounter = n - 1;
while (iterationsCounter) {
currentValue += previousValue;
previousValue = currentValue - previousValue;
iterationsCounter -= 1;
}
return currentValue;
}