cover-img

How I solved FizzBuzz in JavaScript

Simple programming task, FizzBuzz.

19 October, 2021

9

9

0

Contributors

What is fizzbuzz?

FizzBuzz is a simple programming task, commonly used in software developer job interviews, to determine whether the job candidate can write code and to understand how they solve simple problems.

Instructions

Write a program that console logs the number from 1 to n. But for multiples of three print "fizz" instead of the number and for the multiples of five print "buzz". For numbers which are multiples of both three and five

The trick is to firstly check if the number is a multiple of 3 AND 5, if so return 'fizzbuzz'. Else check if number is a multiple of 3, if so return 'fizz'. Finally check if the number is a multiple of 5, if so return 'buzz'. If none of these match return the number

After solving the problem in a simple way, I then looked for ways to refactor my original implementation. The first thing that came to mind was taking a don't repeat yourself (DRY) approach using a conditional (ternary) operator:
My next approach was to use recursion. Not sure I would personally use this solution but it was fun to get it working

javascript

learning

algorithms

9

9

0

javascript

learning

algorithms

Delvoid
Developer . Creator . Full Stack

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.