Skip to the content

Project: FizzBuzz

JavaScript · Unit 3: Loops

FizzBuzz is an interview classic: you count from 1, but the multiples of 3 say Fizz, the ones of 5 say Buzz and the ones of both say FizzBuzz.

Watch out: 15 is a multiple of 3 and of 5, so the order of the conditions matters.

const n = 15;
console.log(n % 3 === 0);
console.log(n % 5 === 0);

Prints

true
true

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    This function asks about the multiples of 3 first. What does it print with 15?

  2. 2. Write the code

    Write fizzbuzz(n). Return "FizzBuzz" if n is a multiple of 3 and of 5, "Fizz" if only of 3, "Buzz" if only of 5 and, if not, the number as text with String(n).

  3. 3. Write the code

    You already have the function. Now the person types how far to play. Use a loop to print fizzbuzz of each number, from 1 up to that number included.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →