The “FizzBuzz”interview question implemented in JavaScript

I wanted to take a brief departure from Salesforce related content.
I do frequent some of the coding blogs and websites out there and many developers who also happen to be interviewers complain about the general poor quality of programmers who supposedly cannot solve basic coding problems.

One of these coding problems is the “FizzBuzz” problem. These kind of questions are asked during the very first round of the technical interview and are generally asked to weed out people who cannot program to solve simple problems.
There are different variants of the problem but generally these are the rules:


Print out the numbers from 1 – 20.
The rules:
For numbers divisible by 3, print out “Fizz”.
For numbers divisible by 5, print out “Buzz”.
For numbers divisible by both 3 and 5, print out “FizzBuzz” in the console.
Otherwise, just print out the number.

It is indeed surprising that many programmers do not get it right.

This image copied WITHOUT permission from the webpage to which I have linked to later in this article.

I think that most of these guys can code but in this day and age of internet blogs and Stack Exchance sites along with IDE’s and coding studios, most folks cannot write code from scratch including your’s truly at times!. Its mostly copy-pasted along with auto completes. 🙂

For example, if you are writing a brand new APEX class or APEX trigger and using the Sublime Text editor with MavensMate, it will create the basic template for you. All you need to do is to add your additional logic.

Then there is also the problem of not revising the basics of coding like different operators, built in functions, best practices etc.

I think that coding/programming is an art and it has to be honed by regular practice. I am trying to imbibe this quality in myself and slowly I am beginning to see the positive results of it. I think everyone can become a better programmer with some effort. But that effort has to be constant throughout our life.

A software developer’s life is a hard life in reality. Always solving new problems and challenges and breaking new ground all the time. So it is imperative to stay at the top of our game by practicing coding and keeping in touch with the latest in our fields of interest.

Anyways I wanted to post a simple implementation of the Fizz Buzz problem in JavaScript based on the rules above. As always I welcome your comments.

This is the output:

FizzBuzz-output

For further reading here is a great article about the FizzBuzz problem and thoughts around it.
Finally good luck with your interviews. Don’t fret if you miss you out on easy questions like these. Nobody is perfect and everybody gets tripped over by little stuff at times. Everyone has bad days. Pick yourself up and move on to the next challenge.