From 29ae11b58c59d3264d943c421cfa052e82b3c3ec Mon Sep 17 00:00:00 2001 From: Caio Thurler Date: Mon, 31 Oct 2022 14:39:52 -0300 Subject: [PATCH] refactor: funcao fibonacci --- 04-fibonacci/index.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/04-fibonacci/index.js b/04-fibonacci/index.js index 61a3be4..51e6399 100644 --- a/04-fibonacci/index.js +++ b/04-fibonacci/index.js @@ -1,10 +1,18 @@ export function fibonacci(value) { // implementar logica aqui - if (value < 1) { - return 0 + if (value < 1) { + return 0; } else if (value <= 2) { - return 1 - } else { - return (fibonacci(value - 1) + fibonacci(value - 2)) - } + return 1; + } else { + let previous = 0; + let next = 1; + let total; + for (let i = 2; i <= value; i++) { + total = next + previous; + previous = next; + next = total; + } + return total; + } } \ No newline at end of file