Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
JS Classic Fibonacci Challenge - Differences between two solutions

I have two solutions to the same challenge, this classic fibonacci challenge that everyone knows how to solve it (even your pets).

I kindly ask you NOT to suggest any other solutions. I just want to compare these two solutions. Thousands different solutions can be found with searches.

Challenge:

/*
     0  1  2  3  4  5  6  7   8   9 
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

     fib(3) returns 2
     fib(9) returns 34
     and so on...

*/

Both solutions below are working fine. My only question is:

Does solution B run slower than solution A? Because in B we have this line below:

fibArr.push(fibArr[fibArr.length - 1] + fibArr[fibArr.length - 2])

Does the length function go through the entire array to calculate the number of items? Or already returns from immediately?

Solution A:

function fib(n) {
  const fiboArray = [0,1]
  for(let i=2; i <= n; i++) {
    fiboArray.push(fiboArray[i-2] + fiboArray[i-1])
  }
  return fiboArray[n]
}
console.log(fib(5))

Solution B:

function fib(n) {
  const fibArr = [0, 1, 1]
  
  if(n == 0) {
    return 0
  }

  if(n == 1 || n == 2) {
    return 1
  }


  if (n > 2) {
    for (let i = 3; i <= n; i++) {
      fibArr.push(fibArr[fibArr.length - 1] + fibArr[fibArr.length - 2])
    }
  }
  
  return fibArr[fibArr.length - 1]
}


console.log(fib(9))
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I concur with CertainPerformance, Solution A is better.

In many situations using .length would be just as fast because the browser will pre-compute it and go just as efficiently as if you make a local variable yourself however I think in your case Solution A is better because you use push on the array during the loop so length will be recalculated.

The answer to this post talks about it but he doesn't have push like you do.

about 4 years ago · Juan Pablo Isaza Relatório

0

@MisterJojo can you please show me a code example to simplify it?

function my_Fibonacci(n)
  {
  let a = 0
    , b = 1
    , r = [0, 1]
    ;
  for(let i=2; i<=n; i++)
    {
    r.push(a+b) // new fibonacci value
    a = b       // set a for next addition
    b = r[i]    // set b for next addition
    }
  // return r.join(' - ')
  return b
  }

document.write(my_Fibonacci(9))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda