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

Scorri per mostrare il menu

book
Challenge: Printing Fibonacci Sequence

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 6. Capitolo 4
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

book
Challenge: Printing Fibonacci Sequence

Compito

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 6. Capitolo 4
Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt