DEV Community

Cover image for JS Coding Question #7: Classic Fizz Buzz (one-liner 🤯)
Let's Code
Let's Code

Posted on • Edited on

5 1

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

Sample Output:

fizzbuzz

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) 
    }
  }
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 👍😊

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (6)

Collapse
 
sankarsubbu profile image
SankarSubbu

All are nice and easy to learn and implement

Collapse
 
corebugcreator profile image
corebugcreator

Image description
I hate if..else, so i came up with this.

Collapse
 
frontendengineer profile image
Let's Code

i try to limit if/else as well if possible. Solution looks good. O(n)

Collapse
 
frontendengineer profile image
Let's Code • Edited

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?

 
frontendengineer profile image
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.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
Comment hidden by post author

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

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay