From 74da8d46ae74e340387b8141867a0e1ace89f782 Mon Sep 17 00:00:00 2001 From: ManuelaLuanaSchumackerTavares Date: Mon, 31 Oct 2022 14:19:47 -0300 Subject: [PATCH] refactor(desafio): Otimizacao da sequencia de fibonnaci --- 04-fibonacci/index.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/04-fibonacci/index.js b/04-fibonacci/index.js index 984c5e6..ad1783a 100644 --- a/04-fibonacci/index.js +++ b/04-fibonacci/index.js @@ -1,9 +1,19 @@ export function fibonacci(value) { // implementar logica aqui - if(value < 2){ - value = value; - }else{ - value = fibonacci(value - 1) + fibonacci(value - 2); + if(value == 0){ + return value; } - return value; + let firstValue = 0, secondValue = 1, thirdValue =1; + for(let i = 2; i <= value; i++){ + thirdValue = firstValue + secondValue; + firstValue = secondValue; + secondValue = thirdValue; + } + return thirdValue; + // if(value < 2){ + // value = value; + // }else{ + // value = fibonacci(value - 1) + fibonacci(value - 2); + // } + // return value; } \ No newline at end of file