Programming Basics: Your First Steps

Lesson 5 of 5

Final Project: FizzBuzz

FizzBuzz is a classic beginner exercise, and a great way to prove you can combine loops and conditionals.

Requirements

  1. Loop through the numbers 1 to 100.
  2. For multiples of 3, print "Fizz" instead of the number.
  3. For multiples of 5, print "Buzz" instead of the number.
  4. For multiples of both 3 and 5, print "FizzBuzz".
  5. For every other number, print the number itself.

Getting started

JavaScript

The % (modulo) operator gives you the remainder of a division, i % 3 === 0 is true exactly when i is a multiple of 3.

Stretch goals

  • Rewrite it as a function fizzbuzz(n) that returns the array of results instead of printing directly.
  • Support a custom range, e.g. fizzbuzz(1, 50).
  • Add a third rule of your own (e.g. multiples of 7 print "Bazz") and combine it correctly with the existing ones.

Submit your code (a repo link or gist) below when you're done, an instructor will review it before you can mark this lesson complete. Welcome to Kodstigen, ready to keep going? 🚀

This lesson ends in a project. Build it on your own machine, there's nowhere to submit it here, but the brief above is everything you need.