Challenge: Printing Fibonacci Sequence
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
andb
which represent the first two terms of the Fibonacci sequence. Initializea
to0
andb
to1
. - 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 terma
. - Define and initialize a variable called
nextTerm
to the value ofa + b
. - Set the value of
a
equal tob
. - Set the value of
b
equal tonextTerm
. - Decrement the value of
numTerms
.
- Use
- Stop the loop once
numTerms
becomes less than0
.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in simpler terms?
What are the main points I should remember?
Can you give me an example?
Awesome!
Completion rate improved to 1.33
Challenge: Printing Fibonacci Sequence
Swipe to show menu
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
andb
which represent the first two terms of the Fibonacci sequence. Initializea
to0
andb
to1
. - 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 terma
. - Define and initialize a variable called
nextTerm
to the value ofa + b
. - Set the value of
a
equal tob
. - Set the value of
b
equal tonextTerm
. - Decrement the value of
numTerms
.
- Use
- Stop the loop once
numTerms
becomes less than0
.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 1.33single