forked from M3-Academy/challenge-algorithms-v2.0
15 lines
340 B
JavaScript
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;
|
|
} |