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

543
Visualizações
How to write a function generating Fibonacci series smaller than a given number (javascript)?

Here's my code:

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1,1]
  let i = 2;
  function fibsRange(i) {
    arr[i] = arr[i-1] + arr[i-2]
    if (arr[i] < num) {
      fibsRange(i+1);//call function one more time;
    }
      return arr
  }
  return fibsRange();      
}
console.log(fibs(5)); 

Assume that given number (num) is bigger than 2. Where do I get wrong?

Note: I edited my code.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

first :as mentionned by Phil your function (the first one) needs to return something. second: you don't need 2 functions, one will do the job. here's a working version of your code (I also modified some unnecessary code.

Edit: I added the case when you need to parse just one parameter

function fibsRange(i,arr,num) {
  if(arr[i-1]+arr[i-2] > num) return arr
  else{
    arr[i] = arr[i - 1] + arr[i - 2]
    return fibsRange(i + 1,arr,num);
  }
}
//in case you need just one parameter
function fib(num){
  let arr = [1, 1];
  let i = 2;
  return fibsRange(i,arr,num);
}

//those(declaration of arr and i) are unnecessary in case you use fib
let arr = [1, 1]
let i = 2;
console.log(fibsRange(i,arr,5));
console.log(fib(5));

about 4 years ago · Juan Pablo Isaza Relatório

0

You never called your fibsRange() function and never returned anything from fibs().

The following version works.

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1, 1]
  if (num>1) fibsRange(2); 

  function fibsRange(i) {
    arr[i] = arr[i - 1] + arr[i - 2]
    if (arr[i] < num) {
      fibsRange(i + 1); //call function one more time;
    }       
  }
  return arr;
}
console.log(fibs(5)); //undified;

about 4 years ago · Juan Pablo Isaza Relatório

0

Your code is checking if arr[i] < num, then invoke the fibsRange(i+1). This is a wrong logic, you will always get the fibbonacci number just higher than the num because you are already setting it at arr[i] and then checking the condition. However, you should first check if arr[i] < num, and then push it into arr.

function fibs(num) {
  //generate Fibonacci numbers:
  let arr = [1, 1];
  let i = 2;
  function fibsRange(i) {
    const lastFibNumber = arr[i - 1] + arr[i - 2];
    if (lastFibNumber < num) {
      arr.push(lastFibNumber);
      fibsRange(i + 1); //call function one more time;
    }
    return arr;
  }
  return fibsRange(i);
}

console.log(fibs(5));
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