Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Printing Fibonacci Sequence | Section
JavaScript Fundamentals
SectionΒ 1. ChapterΒ 68
single

single

bookChallenge: Printing Fibonacci Sequence

Swipe to show menu

Task

Swipe to start coding

The Fibonacci sequence is a series of numbers where each term is the sum of the previous two. The sequence starts with:

0, 1, 1, 2, 3, 5, 8, 13, ...

Your task is to generate the first n Fibonacci numbers.

Instructions:

  1. Initialize:
    let a = 0;
    let b = 1;
    let numTerms = 10;
    
  2. Use a while loop to generate the sequence.
  3. In every iteration:
    • Add the current value of a to an array.
    • Create a variable nextTerm = a + b.
    • Update a = b.
    • Update b = nextTerm.
    • Decrement numTerms.
  4. Return the full Fibonacci sequence as an array.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 68
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt