challenge-algorithms-v2.0-a.../04-fibonacci/index.js

15 lines
314 B
JavaScript

export function fibonacci(value) {
let beforeLastNumber = 0;
let lastNumber = 1;
let nextNumber;
if (value < 2) return value;
for (let i = 2; i <= value; i++) {
nextNumber = lastNumber + beforeLastNumber;
beforeLastNumber = lastNumber;
lastNumber = nextNumber;
}
return lastNumber;
}