Outputs a numerical value from 1 to 100. At that time, it outputs Fizz when it is a multiple of 3, Buzz when it is a multiple of 5, and FizzBuzz when it is a multiple of 15.
Create a fizz_buzz method to solve this problem.
def fizz_buzz
1.upto(100) do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0 && i % 5 != 0
puts "Fizz"
elsif i % 3 != 0 && i % 5 == 0
puts "Buzz"
else
puts i
end
end
end
I felt that it was a problem that could dig deeper into the two perspectives (collective idea of mathematics and the specifications of the ruby program) of how to combine conditional expressions and how much the program reflects them.
Recommended Posts