The FizzBuzz problem that appeared in yesterday's drill.
As a model answer,
def fizz_buzz
num = 1
while num <= 100 do
if num % 15 == 0
puts "FizzBuzz"
elsif num % 3 == 0
puts "Fizz"
elsif num % 5 == 0
puts "Buzz"
else
puts num
end
num = num + 1
end
end
fizz_buzz
That was the correct answer, When I used rubocop,
def fizz_buzz
num = 1
while num <= 100
if (num % 15).zero?
puts 'FizzBuzz'
elsif (num % 3).zero?
puts 'Fizz'
elsif (num % 5).zero?
puts 'Buzz'
else
puts num
end
num += 1
end
end
fizz_buzz
In the form of Enclose the formula in () and judge "whether or not the value is 0", Another description (accidentally) using the zero? method was made.
As for rubocop, I think it would be nice to have a clean and tidy description as much as possible.
Recommended Posts