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
aandbwhich represent the first two terms of the Fibonacci sequence. Initializeato0andbto1. - You have a variable
numTermswhich will represent the number of terms to output. - Use a
whileloop to print the Fibonacci sequence. In every iteration:- Use
console.logto output the current terma. - Define and initialize a variable called
nextTermto the value ofa + b. - Set the value of
aequal tob. - Set the value of
bequal tonextTerm. - Decrement the value of
numTerms.
- Use
- Stop the loop once
numTermsbecomes less than0.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 1.33
Challenge: Printing Fibonacci Sequence
Desliza para mostrar el menú
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
aandbwhich represent the first two terms of the Fibonacci sequence. Initializeato0andbto1. - You have a variable
numTermswhich will represent the number of terms to output. - Use a
whileloop to print the Fibonacci sequence. In every iteration:- Use
console.logto output the current terma. - Define and initialize a variable called
nextTermto the value ofa + b. - Set the value of
aequal tob. - Set the value of
bequal tonextTerm. - Decrement the value of
numTerms.
- Use
- Stop the loop once
numTermsbecomes less than0.
Solución
¡Gracias por tus comentarios!
single