Breaking down a classic interview question: FizzBuzz
Tags: javascript, beginners, career, webdev
Published: 2020-12-13 21:09:36

**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.
``` javascript
function fizzBuzz(n) {
  for (let i = 1; i <=n; i++) {
  // here, we'll check each number for our requirements..
  }
}
```

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)

## 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.
``` javascript
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');
    }
  }
}
```
**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.
``` javascript
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');
    }
    
  }
}
```
## STEP 4
Check to see if we have a multiple of five.
``` javascript
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')
    }
    
  }
}
```

##STEP 5
If we meet **none** of those conditions, we'll just print out that number. 
``` javascript
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)
    }
    
  }
}
```
I hope this was helpful for those just getting started with these kinds of problems :) 

Photo by [Anthony](https://www.pexels.com/@inspiredimages?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels) from Pexels