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

172
Visualizações
Fibonacci with two functions

I amb trying to create a Fibonacci sequence with two functions. Can you tell me where am I wrong, please?

Thank you!

function aurea(quantitatNum){
    let numFibo = [];

    numFibo[0] = 0, numFibo[1] = 1;

    for (i = 2; i < quantitatNum; i++) {
        numFibo[i] = numFibo[i - 2] + numFibo[i - 1];
        numFibo.push(i); 
        numFibo.pop(quantitatNum); 
    }
    return aureaNum;
}

function calcular(){
    let quantitatNum = Number(document.getElementById("quantitatNum").value);
    let dadaCorrecta = Number.isInteger(quantitatNum) && quantitatNum > 0 ? true : false;
    let resposta = document.getElementById("resposta");
    let aureaNum;

    if (dadaCorrecta){
        aureaNum = aurea(quantitatNum);
        resposta.innerHTML = aureaNum;

    } else {
        resposta.innerHTML = "Error! Introdueix un valor enter positiu!";
    }
}

Well, at the begining I created the following structure and it works. But when I tryied insert another function it didn't work...

function calcular(){
    let quantitatNum = Number(document.getElementById("quantitatNum").value);
    let dadaCorrecta = Number.isInteger(quantitatNum) && quantitatNum > 0 ? true : false;
    let resposta = document.getElementById("resposta");
    let numFibo = [];

    numFibo[0] = 0, numFibo[1] = 1;


    if (dadaCorrecta){
        for (i = 2; i < quantitatNum; i++) {
            numFibo[i] = numFibo[i - 2] + numFibo[i - 1];
            numFibo.push(i); 
            numFibo.pop(quantitatNum); 
        }
        resposta.innerHTML = numFibo;
    } else {
        resposta.innerHTML = "Error! Introdueix un valor enter positiu!";
    }
}
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Some issues:

  • return aureNum makes no sense: that is not a variable that is declared in aurea, nor has it participated in the calculation.
  • numFibo.push(i): why would you push the index in this array? The previous line already adds the ith value to the array.
  • pop does not take an argument. No idea what you try to do here. However, after the loop, you could use pop to extract the last summed value and return it.
  • Depending on how you number Fibonacci numbers, the loop should continue until <=.

Here is a corrected version:

function aurea(quantitatNum){
    let numFibo = [];

    numFibo[0] = 0, numFibo[1] = 1;

    for (i = 2; i <= quantitatNum; i++) {
        numFibo[i] = numFibo[i - 2] + numFibo[i - 1];
    }
    return numFibo.pop();
}

function calcular(){
    let quantitatNum = Number(document.getElementById("quantitatNum").value);
    let dadaCorrecta = Number.isInteger(quantitatNum) && quantitatNum > 0 ? true : false;
    let resposta = document.getElementById("resposta");
    let aureaNum;

    if (dadaCorrecta){
        aureaNum = aurea(quantitatNum);
        resposta.innerHTML = aureaNum;

    } else {
        resposta.innerHTML = "Error! Introdueix un valor enter positiu!";
    }
}
<input id="quantitatNum"><button onclick="calcular()">Do</button>
<div id="resposta"></div> 

about 4 years ago · Santiago Gelvez 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