Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!