The FizzBuzz Method
Part of the Fundamentals section of Coddy's Ruby journey — lesson 55 of 88.
Challenge
EasyIn the previous lesson, you created a check_divisible method. Now, build the core fizzbuzz method that determines what to output for a single number.
Create a method called fizzbuzz that takes one argument: n. The method should return:
"FizzBuzz"ifnis divisible by both 3 and 5"Fizz"ifnis divisible by 3 only"Buzz"ifnis divisible by 5 only- The number itself (as a string) if none of the above apply
Use .to_s to convert the number to a string when returning the number itself.
Read a number from input, convert it to an integer value, and call your fizzbuzz method. Print the returned result.
For example, if the input is:
15The output should be:
FizzBuzzIf the input is:
9The output should be:
FizzIf the input is:
10The output should be:
BuzzIf the input is:
7The output should be:
7Try it yourself
# Read input
number = gets.chomp.to_i
divisor = gets.chomp.to_i
# TODO: Create a method called check_divisible that takes two arguments:
# number and divisor. Return true if number is divisible by divisor, false otherwise.
def check_divisible(number, divisor)
return number % divisor == 0
end
# Call your method and print the result
puts check_divisible(number, divisor)All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False