Okay, so I was reading an article that most applicants for programming jobs can’t write a simple algorithm. The one in question is very simple, it’s called Fizz Buzz. Basically, you run the an iteration of how ever many numbers on the console – with every third iteration you substitute the number for the word “Fizz” and every 5th iteration you substitute the number with the word “Buzz”. If you have an iteration that meets both requirements, you substitute the number for the word “FizzBuzz”. Simple enough, right?

Personally, I don’t understand why the problem is that hard. I would feel comfortable writing the algorithm in Java and Javascript, but that’s where my knowledge runs out. Here’s how I implemented my logic in JS:

Obvious for loop is obvious. Now the logic here was that I wanted to test for a condition that would produce FizzBuzz first. FizzBuzz needs to be tested for first because of the nature of If Else statement. It would have been easy to clean this up with my If/Else solution by making the first conditional more efficient. By giving the operator two conditions to test, I’m actually slowing down the process. I should have made the process quicker:

if (i % 15 == 0) {

Now this is better but its not much better and I’ll tell you why. Whenever a conditional operator tests a given condition, it is always going to return a Boolean value. Boolean values have two states true and false, 1 and 0. So, the modulus produces 0 which means that the condition is always going to produce false. So, knowing that, how can I rewrite my code that that I use the Boolean value to my advantage?

If I want to delete the ==0 from my code, then I will need to switch the structure of my If Else statement so that (i%5) prints “Fizz” and ( i%3) produces “Buzz” since I’m reversing the logic. However I will not be able to keep my i%15 condition if I want to use this logic. So that creates another problem. The solution comes in the form of a ternary operator.

Ternary Operators all over that FizzBuzz

Implementing ternary operators in your Javascript will instantly make you feel better. The action of throwing a ? into your code with intent makes you feel like you know what you’re doing. And how! So, basically, I need to rewire my If Else statement and then boil it down into a successful ternary operator. So that I didn’t get lost, I just reverse engineered the solution with a little help from my friends. Here’s the new If/Else statement with comments:

So that is very readable and explains the conditions properly. I ended up getting rid of the comments after I understood what my objective was and I eliminated all the whitespace from the statement. It ended up looking like this:

if (i%3) { if (i%5) { console.log(i); } else { console.log('Buzz'); } else { if (i%5) { console.log('Fizz'); } else { console.log('FizzBuzz');

Since I know that this statement is going to be looped, it makes sense to take out all the console.log statements and create a condition that is going to evaluate and print for every iteration. The skeleton statement starts to look like this:

if (i%3) { if (i%5) { i } else { 'Buzz' } else { if (i%5) { 'Fizz' } else {'FizzBuzz};

This wouldn’t pass for a Yoast readability score but it gets the job done. What the statement is doing is evaluating for “FizzBuzz” last because of the nature of the Boolean values. Basically, I want to know what will happen if (i%3) is false and then what will happen if (i%5) is false. That’s how I’m getting to the basic i value of the loop. The Else statement that comes next evaluates that (i%3) is false but (i%5) is true which is how I’m producing “Buzz”. Else Fizz, else FizzBuzz – you get the picture. However, I still need to turn this into a ternary operation.

So I’ll replace every } else { with a : and every If (...) { with a (...)? and then see if that evaluates my statement the way that I’d like it to. I threw that into a loop and then I produced a working FizzBuzz javascript for loop using ternary operators:

for(i=1;i<=20;i++) {

console.log(((i%3)?(i%5)?i:'Buzz':(i%5)?'Fizz':'FizzBuzz'));

};

Hopefully that helps, it took me all night!

Inspired by!