DEV Community

Cover image for Breaking down a classic interview question: FizzBuzz
Allison Cortez
Allison Cortez

Posted on

Breaking down a classic interview question: FizzBuzz

Problem:
write a program that console logs the numbers from 1 to n.
For multiples of 3 print 'fizz' instead of the number. For multiples of 5 print 'buzz'. For numbers that are multiples of both 3 and 5, print 'fizzbuzz'.

Example:
fizzBuzz(5)
1
2
fizz
4
buzz

The first thing that popped into my head:
We have to iterate through our list of numbers in some way, in order to check each individual number from one to n, which means...

STEP 1, A FOR LOOP

We'll assign i to start at one, since this specific problem requires us to do so.

function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
  // here, we'll check each number for our requirements..
  }
}
Enter fullscreen mode Exit fullscreen mode

The next thing to think about would be how exactly would you calculate a multiple of a number?

Here's a helpful link, in case anyone is lost right now:
MDN Docs

STEP 2, THE MODULO OPERATOR

The first check to make would be to see if our current number in the loop is a modulo of both 3 and 5.

function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
    //is the number a multiple of 3 and 5?
    if (i % 3 = 0 && i % 5 === 0){
    console.log('fizzbuzz');
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Sidenote
We DO NOT set up another separate if statement... we want to build on that first check.

What this means:
If our first if statement is true, we don't run any other code inside the for loop. To ensure this, we'll add onto the first if statement with else ifs.

STEP 3

Check to see if we have a multiple of three.

function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
    //is the number a multiple of 3 and 5?
    if (i % 3 = 0 && i % 5 === 0){
    console.log('fizzbuzz');
    } else if (i % 3 === 0) {
      // is the number a multiple of 3?
        console.log('fizz');
    }

  }
}
Enter fullscreen mode Exit fullscreen mode

STEP 4

Check to see if we have a multiple of five.

function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
    //is the number a multiple of 3 and 5?
    if (i % 3 = 0 && i % 5 === 0){
    console.log('fizzbuzz');
    } else if (i % 3 === 0) {
      // is the number a multiple of 3?
      console.log('fizz');
    } else if (i % 5 === 0) {
      // is the number a multiple of 5?
      console.log('buzz')
    }

  }
}
Enter fullscreen mode Exit fullscreen mode

STEP 5

If we meet none of those conditions, we'll just print out that number.

function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
    //is the number a multiple of 3 and 5?
    if (i % 3 = 0 && i % 5 === 0){
    console.log('fizzbuzz');
    } else if (i % 3 === 0) {
      // is the number a multiple of 3?
      console.log('fizz');
    } else if (i % 5 === 0) {
      // is the number a multiple of 5?
      console.log('buzz')
    } else {
      // none of the conditions above returned true
      // print out the current number we're looping through
      console.log(i)
    }

  }
}
Enter fullscreen mode Exit fullscreen mode

I hope this was helpful for those just getting started with these kinds of problems :)

Photo by Anthony from Pexels

Top comments (0)