Menu
Coddy logo textTech

The FizzBuzz Method

Part of the Fundamentals section of Coddy's Ruby journey — lesson 55 of 88.

challenge icon

Challenge

Easy

In 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" if n is divisible by both 3 and 5
  • "Fizz" if n is divisible by 3 only
  • "Buzz" if n is 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:

15

The output should be:

FizzBuzz

If the input is:

9

The output should be:

Fizz

If the input is:

10

The output should be:

Buzz

If the input is:

7

The output should be:

7

Try 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