← Trail· Lesson 5 of 10
See this lesson in JavaScript →
Loops: times tables and FizzBuzz
Repeat steps with for and while using typed counters, and solve FizzBuzz.

A loop repeats while a condition allows. In for (let i: number = 1; i <= 5; i++) the parts create the counter, decide whether to continue and add 1 after each pass. The : number annotation is optional: let i = 1 is inferred.
while (condicao) checks before each pass. Types don’t prevent infinite loops; the playground stops execution after 1.5 s.
Try it
Edit the times-table base or the countdown start and follow each step in the Console.
Execute o código para ver a saída aqui.Exercise
Complete fizzBuzz(numero: number): string: multiples of 3 and 5 return 'FizzBuzz', of 3 'Fizz', of 5 'Buzz', otherwise String(numero).
Every path returns a string, which is why we use String(numero): returning numero would be a number and TypeScript would flag it. Put the most specific condition first. Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o TypeScript e clique em Verificar.
Next step
Typed functions will help separate the rule from the repetition.