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

15 lines
340 B
JavaScript
Raw Normal View History

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;
}