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
How to implement a Fibonacci sequence in JavaScript?

I need to implement a Fibonacci sequence Оthrough a function for my homework. And I need to implement a function so that each subsequent call will output the next number in the sequence. It seems simple if you pass an argument to the function, but I'm not allowed to do that by the assignment. I have implemented this function with an argument, the code is shown below:

function helperFibonacci(n) {
  let number1 = 0;
  let number2 = 1;

  for (i = 0; i < n; i++) {
    let current = number1 + number2;
    number1 = number2;
    number2 = current;
    console.log(current);
  }
}

helperFibonacci(2);

Please help me implement this function without passing an argument. thanks!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Change number1 and number2 to global variables, and get rid of the loop.

let number1 = 0;
let number2 = 1;

function helperFibonacci() {
  let current = number1 + number2;
  number1 = number2;
  number2 = current;
  console.log(current);
}

for (let i = 0; i < 10; i++) {
  helperFibonacci();
}

about 4 years ago · Juan Pablo Isaza Report

0

Try with this one , works great , have a good coding :)

 function fib(n) {
 let fibList = [];
 if (n < 2) {
    fibList.push(0);
 } else {
    let prev = 0;
    let curr = 1;
    fibList.push(prev, curr);
    for (let i = 2; i < n; i++) {
        const next = prev + curr;
        prev = curr;
        curr = next;
        fibList.push(curr);
    }
 }
 console.log(fibList);
 }
about 4 years ago · Juan Pablo Isaza Report

0

OPTION 1 USING GENERATOR FUNCTIONS

I think the proper way to do this is to use a Generator function. Theese are functions meant to return sequences, they are built for this kinds of situations where you have a sequence that you can calculate based on a formula and want to get the next value at will.

So lets say you have a simple formula to calculate the nth fibonacci number

function fibonacci(n = 0) {
    if (n < 0){ throw 'Fibonacci not defined for negative numbers'} 
    if (n < 2) { return n};
    return fibonacci(n - 1) + fibonacci(n - 2); 
}

so then you just wrap it in a generator function (using the function* syntax, note the *)

function* fibonacciGenerator() {
  var index = 0;
  while (true){
        // yields the next fibonacci
        yield fibonacci(index++);
    }
}

Then you can use it creating a new generator and calling the next function to retrieve the next element in the sequence. Note the while true here represents that the sequence is infinite, that is that it never ends and I can keep getting more and more elements. This does not become an infinite loop because of the yield keyword

let fibonacciSequence = fibonacciGenerator();
fibonacciSequence.next().value; // 0
fibonacciSequence.next().value; // 1
fibonacciSequence.next().value; // 1
fibonacciSequence.next().value; // 2

You can read more about generator functions here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*

NOT USING GENERATOR FUNCTIONS

If you are not familiar with generator functions or cannot use them, you can do it with a regular function and save the "state". I dont recommend at all saving it in a global variable because its a considered bad practice. But you can create a closure.

Closures are like functions with their own scope, or environment where they can have variables.

function fibonacciGenerator() {
    var index = 0;
    return function () {
        return fibonacci(index++);
    }
}

then use it

let fibonacciSequence = fibonacciGenerator();
fibonacciSequence(); // 0
fibonacciSequence(); // 1
fibonacciSequence(); // 1
fibonacciSequence(); // 2

note that the structure is similar than that of generators, but we have normal functions instead.

You can read more about closures here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

about 4 years ago · Juan Pablo Isaza 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!