Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Printing Fibonacci Sequence | Discovering Loops
Introduction to JavaScript

Swipe to show menu

book
Challenge: Printing Fibonacci Sequence

Task

Swipe to start coding

The Fibonacci sequence is a famous sequence of numbers where each term is the sum of the previous two terms.

The first few elements of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

The beginning two elements of the sequence (0, 1) are fixed, and each subsequent term is calculated by summing two preceding ones.

Write a program that outputs the first n Fibonacci numbers.

  • You have two variables a and b which represent the first two terms of the Fibonacci sequence. Initialize a to 0 and b to 1.
  • You have a variable numTerms which will represent the number of terms to output.
  • Use a while loop to print the Fibonacci sequence. In every iteration:
    • Use console.log to output the current term a.
    • Define and initialize a variable called nextTerm to the value of a + b.
    • Set the value of a equal to b.
    • Set the value of b equal to nextTerm.
    • Decrement the value of numTerms.
  • Stop the loop once numTerms becomes less than 0.

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Β 6. ChapterΒ 4

Ask AI

expand
ChatGPT

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

book
Challenge: Printing Fibonacci Sequence

Task

Swipe to start coding

The Fibonacci sequence is a famous sequence of numbers where each term is the sum of the previous two terms.

The first few elements of the sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

The beginning two elements of the sequence (0, 1) are fixed, and each subsequent term is calculated by summing two preceding ones.

Write a program that outputs the first n Fibonacci numbers.

  • You have two variables a and b which represent the first two terms of the Fibonacci sequence. Initialize a to 0 and b to 1.
  • You have a variable numTerms which will represent the number of terms to output.
  • Use a while loop to print the Fibonacci sequence. In every iteration:
    • Use console.log to output the current term a.
    • Define and initialize a variable called nextTerm to the value of a + b.
    • Set the value of a equal to b.
    • Set the value of b equal to nextTerm.
    • Decrement the value of numTerms.
  • Stop the loop once numTerms becomes less than 0.

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Β 6. ChapterΒ 4
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt