Algorithm 101: 4 Ways to FizzBuzz a Single Number
Tags: algorithms, javascript, beginners, fizzbuzz
Published: 2020-03-24 11:57:14

Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word ``fizz``, any number divisible by five with the word ``buzz`` and numbers divisible by 15 (i.e. both 3 and 5) become ``fizz buzz``. - [Wikipedia](https://en.wikipedia.org/wiki/Fizz_buzz).

In this article, we will be focusing on a Single entry. In a [future article](https://dev.to/ebereplenty/algorithm-101-2-ways-to-fizzbuzz-a-range-of-numbers-8f8), we will look at a range of numbers.

```javascript

fizzBuzz(12) // 'Fizz'

fizzBuzz(15) // 'FizzBuzz'

fizzBuzz(25) // 'Buzz'

```

### Prerequisite
To flow with this article, it is expected that you have basic understanding of JavaScript's arithmetic and selective constructs.

### Let's FizzBuzz Using:

* if...else statement (1)

```javascript
      function fizzBuzz(num) {
        function multipleOf3(n) {
          return n % 3 === 0;
        }

        function multipleOf5(n) {
          return n % 5 === 0;
        }

        if (multipleOf3(num) && multipleOf5(num)) {
          return "FizzBuzz";
        }

        if (multipleOf3(num)) {
          return "Fizz";
        }

        if (multipleOf5(num)) {
          return "Buzz";
        } else {
          return num;
        }
      }
```

* if...else statement (2)

```javascript
      function fizzBuzz(num) {
        function multipleOf3(n) {
          return n % 3 === 0;
        }

        function multipleOf5(n) {
          return n % 5 === 0;
        }

        function multipleOf15(n) {
          return n % 15 === 0;
        }

        if (multipleOf15(num)) {
          return "FizzBuzz";
        } else if (multipleOf3(num)) {
          return "Fizz";
        } else if (multipleOf5(num)) {
          return "Buzz";
        } else {
          return num;
        }
      }
```

* switch...case statement (1)

```javascript
      function fizzBuzz(num) {
        function multipleOf3(n) {
          return n % 3 === 0;
        }

        function multipleOf5(n) {
          return n % 5 === 0;
        }

        switch (multipleOf3(num) && multipleOf5(num)) {
          case true:
            return "FizzBuzz";
            break;
        }

        switch (multipleOf3(num)) {
          case true:
            return "Fizz";
            break;
        }

        switch (multipleOf5(num)) {
          case true:
            return "Buzz";
            break;
          default:
            return num;
        }
      }
```

* switch...case statement (2)

```javascript
      function fizzBuzz(num) {
        function multipleOf3(n) {
          return n % 3 === 0;
        }

        function multipleOf5(n) {
          return n % 5 === 0;
        }

        function multipleOf15(n) {
          return n % 15 === 0;
        }

        switch (multipleOf15(num)) {
          case true:
            return "FizzBuzz";
            break;
        }

        switch (multipleOf3(num)) {
          case true:
            return "Fizz";
            break;
        }

        switch (multipleOf5(num)) {
          case true:
            return "Buzz";
            break;
          default:
            return num;
        }
      }
```

### Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. Feel free to let me know other ways you solved yours in the comment section.

> Up Next: [Algorithm 101: 2 Ways to FizzBuzz a Range of Numbers](https://dev.to/ebereplenty/algorithm-101-2-ways-to-fizzbuzz-a-range-of-numbers-8f8)

If you have questions, comments or suggestions, please drop them in the comment section.

You can also follow and message me on social media platforms. 

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.