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

Glissez pour afficher le menu

book
Challenge: Printing Fibonacci Sequence

Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 4
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

book
Challenge: Printing Fibonacci Sequence

Tâche

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 desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 6. Chapitre 4
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt