Experimenting with FizzBuzz
The FizzBuzz is a game for children to teach them about division. It has been (and still is) used in programming interviews sort out the bad programmers.
The rules are simple:
- You count from 1 to 100.
- When it’s a multiple of 3, write Fizz.
- When it’s a multiple of 5, write Buzz.
- When it’s a multiple of 3 and five, write FizzBuzz.
- When none of the rules applies, write the number.
It plays with the basic concepts of programming, operators, conditions and loops.
I’ll detail over here the process I’ll go through to make it work. One first simple and dumb version could go as such :
Problem solved, and yet it can already be re-factored to this with the usage of variables, concatenation and functions :
And then, your boss enters the room, and ask if you can manage multiple of 7 and 9 with “foo” and “bar”.
You could add some more variables and more
if statements but you know you can do better. So you create a function which takes in the numbers you want to work with and returns an array of elements.
This adds a functional approach to the problem and shows some more skills.
Finally if shortness is what you want, you can go one-liner this way :