“FizzBuzz”

Joseph K Abe
2 min readOct 2, 2021

--

As you know I have been brushing up on possible interview questions. One that I came across that seems to be extremely common is a question called “FizzBuzz” In this challange you are given numbers and are asked to crate at function that returns “Fizz” if the number is divisible by 3, “Buzz” if the number is divisible by 5, and “FizzBuzz” if the number is divisible by both 3 and 5. In my opinion this is such a refreshingly fun challange! In this one all we are really going to have to do is write a condition that checks if there is a remainder when the number is divided by 3 or 5 using the remainder operator ( % ). First lets check for numbers that will retrun “FizzBuzz”…

function fizzBuzz(num){if (num % 3 === 0 && num % 5 === 0){return 'FizzBuzz' }}

Ok this will take care of the numbers that are divisible by both 3 and 5. If the number is divisible by 3 or 5 then the remainder will equal 0. Next lets check for the numbers that are only divisible by 3…

function fizzBuzz(num){if (num % 3 === 0 && num % 5 === 0){return 'FizzBuzz'}else if (num % 3 === 0){return 'Fizz' }}

Now we have a condition that will return “Fizz” for numbers that meet our condition of having a remainder of 0 when divided by 3. The final condition is the same as the last on but returning “Buzz” when the remainder when divided by 5 is 0…

function fizzBuzz(num){if (num % 3 === 0 && num % 5 === 0){return 'FizzBuzz'}else if (num % 3 === 0){return 'Fizz'}else if (num % 5 === 0){return 'Buzz'}}}

And there it is! Our fizzBuzz function is working perfectly! Its nice to have a problem that is simple and fun every now and then!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response