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

16 lines
332 B
JavaScript

export function fibonacci(value) {
if(value == 0) return 0;
if(value == 1 || value == 2) return 1;
let fib;
let valorAnterior1 = 0;
let valorAnterior2 = 1;
for(let i = 2 ; i <= value ; i++) {
fib = valorAnterior1 + valorAnterior2;
valorAnterior1 = valorAnterior2;
valorAnterior2 = fib;
}
return fib;
}