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

129
Visualizações
Stop fibonacci sequence when it reaches a number bigger than 1000

I want to make the sequence stop when the last value stored in the array is bigger than 1000.

I have the js code for the sequence and i can make it stop at whatever position i want with the variable limit. But i dont know how to make it stop at a certain number.

The is my code:

let fib = [1,1];
let limit = 20;

function fibonacci(nums) {
    let data = [1,1];
    
    for(let i = 2; i < limit; i++) {
      nums[i] = nums[i - 1] + nums[i - 2];
      data.push(nums[i]);
    }
      
    return data;
      

}

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

0

You can just use an if condition and break out of the loop as soon as a value is bigger than 1000. In this example I pushed the value and then break out of the loop. You can also break before pushing it to the array

let fib = [1, 1];
let limit = 20;

function fibonacci(nums, breakLimit) {
  let data = [1, 1];

  for (let i = 2; i < limit; i++) {
    nums[i] = nums[i - 1] + nums[i - 2];
    if (nums[i] > breakLimit) {
      data.push(nums[i]);
      break;
    } else {
      data.push(nums[i]);
    }
  }
  return data;
}

console.log(fibonacci(fib, 1000))

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