7 Lines to Code Fizz-Buzz Game Principle in Elixir
--
Coding FizzBuzz Game Backend Principles
This article is all about coding the implicit conditions in FizzBuzz. This makes you to walk over lines of code that gives similar output with different syntax.
To whom ?
This is for complete beginners in Elixir. But, this gonna be more informative at the end.
What is Fizz-Buzz ?
When you google, you will find the following phrases about the FizzBuzz.
FizzBuzz is a very simple programming task, used in software developer job interviews, to determine whether the job candidate can actually write code.
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word “fizz”, and any number divisible by five with the word “buzz” and if divisible by both three and five with the word FizzBuzz or else just printing the number itself.
That’s all about the FizzBuzz. Oh.. you finally know what FizzBuzz is . Keep Rocking…
What are we gonna code here?
We simply print the numbers in a range replacing them with words Fizz, Buzz, and FizzBuzz using game protocol.
Time to code…
Fizz Section
Here, I named the module with FizzBuzz
of course everybody does the same and wrote a play/2
function.
At first we just print the numbers in the range and see how it works.
defmodule FizzBuzz do
def play(min, max) do
Enum.each(min..max, fn(num)-> IO.puts num end )
end
end
play/2
This function takes two numbers as input technically minimum and maximum values in a range. So, we can create a range via ../2
macro.
min..max
A range is represented internally as a struct. A range implements the Enumerable
protocol, which means functions in the Enum
module can be used to work with ranges.
You can check the implemented protocols with the help of i/1
i 1..2