Add case-based fizzbuzz

This commit is contained in:
Nathan Mattes 2021-11-29 23:15:49 +01:00
parent a984d9252f
commit c557bf2b6e
1 changed files with 17 additions and 0 deletions

View File

@ -29,3 +29,20 @@ defmodule ElixirFizzbuzz do
def _fizzword(n, _, 0), do: "Buzz"
def _fizzword(n, _, _) when n > 0, do: n
end
defmodule CaseFizzbuzz do
def upto(n) when n > 0 do
1..n
|> Enum.map(&fizzbuzz/1)
end
def fizzbuzz(n) do
current = {n, rem(n, 3), rem(n, 5)}
case current do
{n, 0, 0} -> "Fizzbuzz"
{n, 0, _} -> "Fizz"
{n, _, 0} -> "Buzz"
{n, _, _} -> n
end
end
end