challenge-algorithms-v2.0-v.../04-fibonacci/index.js
2022-10-28 07:55:23 -03:00

15 lines
340 B
JavaScript

export function fibonacci(value) {
let previous0 = 0, previous1 = 1;
let nextValue = 0;
if (value === 0) {
nextValue = 0;
} else if (value === 1) {
nextValue = 1;
}
for (let i = 2; i <= value; i++) {
nextValue = previous0 + previous1;
previous0 = previous1;
previous1 = nextValue;
}
return nextValue;
}