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

Pyyhkäise näyttääksesi valikon

book
Challenge: Printing Fibonacci Sequence

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 4

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

book
Challenge: Printing Fibonacci Sequence

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 6. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt