Created
January 3, 2018 08:27
-
-
Save scrubmx/5d3bd044ae7a56c35bfc94acbb313976 to your computer and use it in GitHub Desktop.
FizzBuzz in Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule FizzBuzz do | |
def print do | |
Enum.each(1..100, &FizzBuzz.print/1) | |
end | |
def print(n) do | |
result = cond do | |
divisible_by_3?(n) && divisible_by_5?(n) -> "FizzBuzz" | |
divisible_by_3?(n) -> "Fizz" | |
divisible_by_5?(n) -> "Buzz" | |
true -> n | |
end | |
IO.puts(result) | |
end | |
def divisible_by_3?(n) do | |
rem(n, 3) == 0 | |
end | |
def divisible_by_5?(n) do | |
rem(n, 5) == 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment