Fizzing and Buzzing

  • In this project, we're going to build FizzBuzz. It's a children's game where you count from 1 to 20. Easy, right?

    Here's the catch: instead of saying numbers divisible by 3, say "Fizz". And instead of saying numbers divisible by 5, say "Buzz". For numbers divisible by both 3 and 5, say "FizzBuzz". "1, 2, Fizz, 4, Buzz"...and so forth.

    This project will walk you through building the game in five steps.

    Let's get started by using console.log to print out all of the numbers from 1 and 20.

    But don't just type out 20 console.log statements by hand—use your new programming knowledge to do it more effectively. You should only use ONE console.log in your code.

    Show hint

    Try using a for loop to write out all the numbers.

    Inside the { } of the for loop, don't forget to put ; at the end of each command.

  • Awesome! Now that we know how to count, let's start playing the game.

    If the number is divisible by 3, print "Fizz".

    If it's not, just print the number.

    Show hint

    3 should divide evenly into your number with no remainder (ie. a remainder of 0). Click here for a reminder on how to deal with remainders.

  • You are zzzzzzzzooming through this tutorial. You must be very buzzzzzzziness like. We've got Fizz, let's add some Buzz.

    Add an else if statement to check if the number is divisible by 5, and if it is, print out "Buzz" instead of the number.

    Show hint

    You can handle more than two options with an else if statement. Get a reminder here.

  • You are fizzing and buzzing right along.

    But look at that "15"...it says "Fizz" but it should be "FizzBuzz" because it's divisible by both 3 and 5. Oops!

    Add another if statement to your code to check for this special case.

    Show hint

    The order of your if statements matters a lot here. Each time your program goes through the for loop, it will check the if conditions in order and do the first one that is true. It will then go on to the next value of i.

    So if you put the i%5===0&&i%3===0 check at the end, your program will never get to it!

    Think about i=15. It will check the first if condition, that is checking that i is divisible evenly by 3. That is true, so it will print "Fizz" and move on to i=16!

    So where should you put the check for 3 and 5? When you're done, make sure the first if block starts with if, the last one starts with else, and every one in between starts with else if.

  • FizzBuzz from 1 to 20 is cool, but you know what's cooler? FizzBuzz from 1 to 100.

    Modify your FizzBuzz program so that it runs for 1 to 100.

    If you want to be really cool, replace 100 with a variable that you declare and set at the beginning of your program so you can easily impress your friends with FizzBuzz solutions for any number of your choosing.

Keyboard shortcuts: Run CTRL + Enter Reset CTRL + S