When I solved the Ruby problem, I was disappointed to see the smartness of the model answer for my answer: confounded :, so I will leave it as a memorandum.
Enter a positive integer. That integer is The difference from multiples of 10 (10,20,30 ...) True if within 2 Other than that, let's create a method that outputs False.
** Output example: ** near_ten(12)→True near_ten(17)→False near_ten(19)→True
Ruby
def near_ten(num)
if num % 10 <= 2
puts "True"
elsif num % 10 >= 8
puts "True"
else
puts "False"
end
end
Ruby
def near_ten(num)
quotient = num % 10
if quotient <= 2 || quotient >= 8
puts "True"
else
puts "False"
end
end
When I saw the model answer, I shouted in my heart that it was ** too smart! (I can't speak out because I'm on the go: unamused :)
Certainly, or" || "It is easy to forget the existence of. .. .. ..
To be honest, the code is correct: sparkles :, but I felt once again that the coolness oozes out depending on how smart: star2: is written. .. ..
Easy to refactor. .. ..
Recommended Posts