Let's Get A Job! - The Fizz Buzz programming test

Part 1: Let's Scratch!
Part 2: Let's Move!
Part 3: Let's Broadcast Variable Messages!
Part 4: Let's Make A Game!

When you apply for a job as a programmer you may be asked to demonstrate your skills by writing a simple piece of code....

Write a program that prints the numbers from 1 to 100 but for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Such tests are done because employers find that some people applying for programming jobs, even after studying programming at university, are unable to write even simple pieces of code; which really should not be a problem for anyone familiar with any programming language.

So how would we go about writing that?

We could just write a long list adding each number, Fizz, Buzz or FizzBuzz as necessary.

That will meet the specified requirements of the test but that was a lot of work - and we are only up to 16! What if the problem changes and they want to do all the numbers up to 200 or 1000? Programmers like to do as little as possible and make the computer do all the work.

We can see that there are two things that vary; the number and the thing to be displayed on the screen. So we can start by creating two Variables, which I have called theNumber and theShout, and setting them to zero.

We use a loop that repeats until theNumber has counted up to 100 by adding a   =  , dropping theNumber into the first hole and typing 100 into the second.

Inside the repeating loop we change theNumber by 1 to increase the value stored in theNumber variable every time we go around the loop. We set theShout to theNumber and say theShout for .5 secs.

Clicking on the script will start it running and the cat will count up to 100 for us. Now we can change that 100 to 200 or 1000 and let the computer do all the hard counting work for us.

We need to test if the value stored in theNumber variable is a multiple of 3 or 5 or both. As you may well remember we use an  If  to test if the condition is true. Scratch provides a  mod  operator, short for modulus, which will give us the remainder left when repeatedly dividing by 3 or 5. If the remainder is 0 we know that theNumber was divided exactly by 3 or 5 and so can set theShout accordingly.

  Note: The remainder is usually called the residue in modulo arithmetic. See this PDF file for an introduction or Wikipedia for more detail if required.

Add an  If  above the say instruction. Drop an  =  into the hole, drop a  mod  to the left of the = sign, adding theNumber and typing 3 after the mod and 0 to the right of the = sign.

If theNumber is a multiple of 3 (theNumber mod 3 = 0) we set theShout to Fizz

Repeat with another  If  but changing 3 to 5 and Fizz to Buzz. We now need a final  If  to check if theNumber is divisible by both 3 and 5. Add an  and  to the  If . That is a logical “and”. It checks that both sides are true. If one side is not true then the test fails. Drop an  =  into both sides of the  and  and then add a  mod  to both sides and complete as above. Set theShout to FizzBuzz.

An alternative approach would be to set two more variables (I called mine fizzing and buzzing) to false at the start of each loop and switch them to true when you find a multiple of 3 or 5. You can then change the final  if  test to see if they are both set to true in which case you set the shout to FizzBang.

Top and tail the script with a green flag starter and a stop script and we are done.

For comparison here is the same code written in the Python language.

Python uses indentation to structure the code. It uses a While loop instead of the Repeat Until. Everything indented beneath the 'while' is included in the loop and everything indented beneath an 'if' is performed if the 'if' statement is true. The value stored in theNumber is incremented by adding 1 to itself. The value stored in theShout is set to the value stored in theNumber. The % character is used instead of the word mod, as it is in many other languages, and the test for equality uses a double == as the single = is used to assign a value to a variable.

This is the same code written in JavaScript.

The main difference is the code is broken up with braces {} and brackets (). The variables are set and we use a While loop with everything between the { and the } included in the loop. Again we use % for mod and == to test for equality. If the If test is true then the instructions between the following { and } are performed otherwise they are ignored. The logical and is replaced with &&. To show the results we use document.write and we need to add a "\n" which makes a new line after theShout has been shown otherwise it would be displayed in a single line across the page: 12Fizz4BuzzFizz78 etc.
As JavaScript is designed to be used in a browser you can see the script in action on its own web page.

Here it is again in Ruby:

This is very similar to the others but we need an 'end' to close our 'If' statements and 'while' loop. We use 'puts' to display the result as this adds a newline for us; had we used 'print' we would need to add the '\n' character ourselves as we did in the JavaScript version.

And finally using the C language (most of which you will be familiar with ;-)

The initial #include tells the computer where to find the Standard Input and Output devices (std io) - these being your screen and keyboard. The whole thing is wrapped up in a Main function (which is what the computer looks for when it runs the code) marked out with an opening { and closing }.

We set our variable theNumber to 100 and declare it to be an int (short for integer). This time we are not using theShout variable but will just insert the text as required.

We drop into a While loop (again marked by an opening { and closing }) and we increment theNumber by adding 1 to itself. We now trickle through a series of If tests. This time we are combining them all with an Else between each test. If the If part fails (is not true) then we go on to the following Else part; if it is true we jump out of the If section and carry on around the loop.

We check if theNumber is a multiple of 3 and 5 if it is we 'puts' FizzBuzz if it is not both we check for multiples of 5 or 3 individualy, if it is neither of those we print theNumber. We use the Printf command (the f stands for 'formatted') which lets you construct a string of text and insert your value. Here it will replace the integer value (%i) with whatever is in our variable theNumber which we include before closing the brackets. We need to add a \n to force a new line; again the puts supplies a newline for us. We finally return a 0 to the main function because it is expecting something.

The results of our efforts looks like this:

As these examples have been specifically written to show the similarities there is not much difference between the five versions. Each language offers variations in the commands available (which is why there are so many programming languages) but they borrow from each other and many commands (or variations thereof) can be found across languages; for example you will find printf commands available in Python and Ruby but languages also evolve so in Ruby, for example, you can turn an 'If" statement inside out so it reads:

Once you are comfortable with the Scratch language you will also be familiar with the basics of many other languages and so there will be less effort required to learn the other languages and getting a job should not be problem.

Note: There is no sound on this version of the video:

Part 1: Let's Scratch!
Part 2: Let's Move!
Part 3: Let's Broadcast Variable Messages!
Part 4: Let's Make A Game!