Fibonacci series in javascript using while loop

(Last Updated On: 28/12/2023)

Dive into the enchanting realm of the Fibonacci series in JavaScript using a while loop. Unlock the magic of sequences as we explore the elegance of this timeless mathematical concept. JavaScript, a powerhouse in web development, takes center stage in crafting Fibonacci patterns. Follow along to unravel the secrets of the while loop and its role in generating captivating numerical sequences.

Fibonacci series in javascript using while loop- code

Learn the Fibonacci series in javascript using while loop given below:

<html>
   <head>
      <title> Fibonacci series in javascript using while loop </title>
   </head>
   <body>
      <script>  
         function generateFibonacciSeries(limit) {
           let a = 0, b = 1;
           let result = [];
         
           if (limit >= 1) {
             result.push(a);
           }
           if (limit >= 2) {
             result.push(b);
           }
         
           let count = 2;
           while (count < limit) {
             let next = a + b;
             result.push(next);
             a = b;
             b = next;
             count++;
           }
         
           return result;
         }
         
         // Generating Fibonacci series up to a limit
         const limit = 20; // Change this limit as needed
         const fibonacciNumbers = generateFibonacciSeries(limit);
         
         document.write("Fibonacci series up to ", limit, " : ", fibonacciNumbers);
      </script>  
   </body>
</html>

Learn How to Generate Fibonacci series in C using for loop, Now!

This HTML document contains JavaScript code to generate the Fibonacci series using a while loop. Here’s an explanation of the code:

  • Function Definition: The `generateFibonacciSeries` function takes a `limit` as an argument to generate Fibonacci numbers up to that limit.
  • Initialization: Variables `a` and `b` are initialized to 0 and 1, respectively. An empty array `result` is created to store the Fibonacci series.
  • Base Cases: The function checks if the limit is greater than or equal to 1 and 2, pushing the initial values (0 and 1) to the result array.
  • While Loop: The while loop continues until the count reaches the specified limit. Inside the loop, the next Fibonacci number is calculated, added to the result array, and variables are updated accordingly.
  • Return Result: The function returns the generated Fibonacci series as an array.
  • Generating and Displaying Series: The code then sets the `limit` to 20 (you can change it) and calls the `generateFibonacciSeries` function. The result is displayed on the web page using `document.write`.

This code demonstrates a modular approach, encapsulating the logic within a function, making it more reusable and readable. The generated Fibonacci series is then displayed on the web page.

Unlock a World of Fibonacci series in javascript using for loop Today!

Output:

Fibonacci series up to 20 : 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181 

Comparison with Other Methods:

Recursion:

  • Advantages: Recursion offers an elegant solution mirroring the mathematical definition of the Fibonacci series.
  • Considerations: However, it may lead to stack overflow for large series due to its synchronous nature.

For Loop:

  • Advantages: For loops provide explicit control over initialization, condition, and iteration, offering simplicity and efficiency.
  • Considerations: The number of iterations must be predetermined, limiting flexibility.

Check out Javascript for Loop, Now!

While Loop:

  • Advantages: While loops offer dynamic iteration, adapting to varying conditions. Their concise structure promotes readability.
  • Considerations: A clear termination condition is crucial to avoid infinite loops.

JavaScript’s while loop emerges as a dynamic tool for crafting Fibonacci series, offering a blend of flexibility and readability. Unleash the power of coding and experiment with the Newtum Online Compiler. Learn, Experiment, and Execute with Newtum!

About The Author