DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Updated on

Algorithm 101: 4 Ways to FizzBuzz a Single Number

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.

In this article, we will be focusing on a Single entry. In a future article, we will look at a range of numbers.


fizzBuzz(12) // 'Fizz'

fizzBuzz(15) // 'FizzBuzz'

fizzBuzz(25) // 'Buzz'

Enter fullscreen mode Exit fullscreen mode

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)
      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;
        }
      }
Enter fullscreen mode Exit fullscreen mode
  • if...else statement (2)
      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;
        }
      }
Enter fullscreen mode Exit fullscreen mode
  • switch...case statement (1)
      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;
        }
      }
Enter fullscreen mode Exit fullscreen mode
  • switch...case statement (2)
      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;
        }
      }
Enter fullscreen mode Exit fullscreen mode

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

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 | LinkedIn | Github

Thank You For Your Time.

Top comments (0)