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

Veeg om het menu te tonen

book
Challenge: Printing Fibonacci Sequence

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 6. Hoofdstuk 4
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

book
Challenge: Printing Fibonacci Sequence

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 6. Hoofdstuk 4
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt