## DEV Community π©βπ»π¨βπ» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Let's Code

Posted on • Updated on

# JS Coding Question #7: Classic Fizz Buzz (one-liner π€―)

#### Interview Question #7:

Write a function that will print from 1 to 100. Print 'fizz' for multiples for 3. Print 'buzz' for multiples of 5. Lastly, print 'fizzbuzz' for multiples of 3 and 5.π€

If you need practice, try to solve this on your own. I have included 2 potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark π even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Code if you want to play around with it: https://codepen.io/angelo_jin/pen/MWobgqj

#### Solution #1: if-else (Recommended)

• A straight-forward approach using the good old if-else statements. It is nice because it is easy to implement and you can code this while you are explaining to the interviewer what is happening statement per statement.
``````  for (let i = 1; i <= 100; 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')
// Is the number a multiple of 5?
} else if (i % 5 === 0) {
console.log('buzz')
} else {
console.log(i)
}
}
``````

#### Solution #2: Nice, fancy one-liner

• I would stay away from this on an actual interview as you would look like a leetcode material/master. You may mention that you saw a one-liner solution using couple of ternary and you are aware. Might get a bonus for that.
``````  for(let i=0;i<100;)console.log((++i%3?'':'fizz')+(i%5?'':'buzz')||i)
``````

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

In case you like a video instead of bunch of code ππ

Luke Shiru

I actually made a post about this last year, and the community came up with some one liners that are way shorter than the one on this post. Check them out here.

Let's Code • Edited on

Nice and thanks for sharing! These one-liner are nice and amazing to see but not worth to spend much time and energy on as one will not present them on an actual interview.

If I go on an interview and get asked to code Fizz Buzz, i would still stick to the old if-else solution. Do you feel the same?

Luke Shiru

tbh, it's been years since I got something like this on an interview (that's why when I do the interviews I don't do this kind of exercises), but my solution on an interview would be something like:

``````const fizzBuzz = n =>
[...Array(n + 1).keys()].map(
index =>
`\${++index % 3 ? "" : "Fizz"}\${index % 5 ? "" : "Buzz"}` || index
);
``````

Mainly because I haven't used a `for` in years now, so doing it for an interview wouldn't make much sense for me.

Let's Code

looks good to me! thanks for sharing.

i hardly use old school `for` iterate as well especially there are other better alternatives.

Jon Randy • Edited on

Some more, but avoiding using the conditional operator...

``````const fizzBuzz = x=>[x,f='Fizz',b='Buzz',f+b][!(x%5)*2+!(x%3)]
const fizzBuzz = x=>({1:x,6:f='Fizz',10:b='Buzz',0:f+b}[x**4%15])
``````

Some comments have been hidden by the post's author - find out more

This post blew up on DEV in 2020:

## πβοΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! π₯³

Happy coding!