Practice Makes Perfect; Methods, Blocks, & Sorting


#1

https://www.codecademy.com/courses/ruby-beginner-en-ET4bU/1/4?curriculum_id=5059f8619189a5000201fbcb#

A by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not.

Why do you put a 0 after ==

def by_three?(number)
if number % 3 == 0
return "true"
else
return "false"
end
end

puts by_three?(2)


#2

Because if the remainder is 0, number is evenly divisible.

 0 % 3 == 0
 3 % 3 == 0
 6 % 3 == 0
 9 % 3 == 0
12 % 3 == 0

and so on.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.