Skip to content

Instantly share code, notes, and snippets.

@dcyoung-dev
Created December 15, 2020 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcyoung-dev/5090e5120f067954ea96d9c4104de289 to your computer and use it in GitHub Desktop.
Save dcyoung-dev/5090e5120f067954ea96d9c4104de289 to your computer and use it in GitHub Desktop.
Fizz Buzz solution in Ruby
# FizzBuzz
def add_trigger_word(number, factor, output_array, word)
output_array << word if number % factor == 0
output_array
end
word_map = {
3 => "Fizz",
5 => "Buzz",
7 => "Bang"
}
(1..100).each do |index|
trigger_words = word_map.reduce([]) { |output, (key, word)| add_trigger_word(index, key, output, word) }
trigger_word = trigger_words.join
puts trigger_word.empty? ? index : trigger_word
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment