Skip to content

Instantly share code, notes, and snippets.

@TaylorOD
Forked from Kerrick/fizzbuzz.rb
Created August 4, 2020 17:04
Show Gist options
  • Save TaylorOD/effda8fc69338861bb47e8d821d8639a to your computer and use it in GitHub Desktop.
Save TaylorOD/effda8fc69338861bb47e8d821d8639a to your computer and use it in GitHub Desktop.
Different solutions for Fizz Buzz in Ruby
def fizz_buzz_1(max)
arr = []
(1..max).each do |n|
if ((n % 3 == 0) && (n % 5 == 0))
arr << "FizzBuzz"
elsif (n % 3 == 0)
arr << "Fizz"
elsif (n % 5 == 0)
arr << "Buzz"
else
arr << n
end
end
return arr
end
def fizz_buzz_2(max)
arr = []
(1..max).each do |n|
if (n % 3 == 0)
if (n % 5 == 0)
arr << "FizzBuzz"
else
arr << "Fizz"
end
elsif (n % 5 == 0)
arr << "Buzz"
else
arr << n
end
end
return arr
end
def fizz_buzz_3(max)
arr = []
(1..max).each do |n|
text = ""
if (n % 3 == 0)
text << "Fizz"
end
if (n % 5 == 0)
text << "Buzz"
end
if !((n % 3 == 0) || (n % 5 == 0))
text = n
end
arr << text
end
return arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment