Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule FizzBuzz do
@doc """
Given a range, returns an array of values conforming to the FizzBuzz pattern.
## Example
FizzBuzz.calculate 1..15
-> 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
"""
def calculate(range) do
Enum.map(range, &(FizzBuzz.calculate_value &1))
end
@doc """
Calculate the FizzBuzz value for a single number
"""
def calculate_value(value) when rem(value, 15) == 0, do: "FizzBuzz"
def calculate_value(value) when rem(value, 3) == 0, do: "Fizz"
def calculate_value(value) when rem(value, 5) == 0, do: "Buzz"
def calculate_value(value), do: value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.