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

16 lines
332 B
JavaScript
Raw Normal View History

export function fibonacci(value) {
2022-10-28 21:38:38 +00:00
if(value == 0) return 0;
if(value == 1 || value == 2) return 1;
let fib;
let valorAnterior1 = 0;
let valorAnterior2 = 1;
2022-10-28 21:38:38 +00:00
for(let i = 2 ; i <= value ; i++) {
fib = valorAnterior1 + valorAnterior2;
valorAnterior1 = valorAnterior2;
valorAnterior2 = fib;
2022-10-28 21:38:38 +00:00
}
return fib;
}