Task: Implement a function which loops from 1 to 100 and prints "Fizz" if the counter is divisible by 3. Prints "Buzz" if the counter is divisible by 5 and "FizzBuzz", when the counter is divisible by 3 and 5.
The instructor gave me the hint, that it's solvable using the ternary operator. Otherwise I would have used an if-then-else approach.
My solution:
def fizz_buzz
for i in 1...100
puts i % 3 == 0 ? i % 5 == 0 ? "FizzBuzz" : "Fizz" : i % 5 == 0 ? "Buzz" : i
end
end
What's your opinion about the approach? Would you still consider it as readable?
I personally consider the readability hard on the border.
Which approach would you prefer and why?
if
: How would you doif-else
? \$\endgroup\$