Menu
Coddy logo textTech

Adding The Twist

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

challenge icon

Challenge

Easy

In the previous lesson, you created a FizzBuzz program that loops through numbers using the default divisors 3 and 5. Now, add the twist by making the divisors customizable through default parameters.

Modify your fizzbuzz method to accept three arguments:

  • n - the number to check (required)
  • fizz_divisor - the divisor for "Fizz" (optional, defaults to 3)
  • buzz_divisor - the divisor for "Buzz" (optional, defaults to 5)

The method should use these custom divisors instead of hardcoded values when checking divisibility.

You will receive three lines of input:

  1. The upper limit n
  2. The fizz divisor
  3. The buzz divisor

Loop from 1 to n and call your updated fizzbuzz method with all three arguments. Print each result.

For example, if the inputs are:

10
2
3

The output should be:

1
Fizz
Buzz
Fizz
5
FizzBuzz
7
Fizz
Buzz
Fizz

If the inputs are:

15
3
5

The output should be:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Try it yourself

# Read input
n = gets.chomp.to_i

# Method from previous lesson
def check_divisible(number, divisor)
  return number % divisor == 0
end

# Create the fizzbuzz method
def fizzbuzz(n)
  if check_divisible(n, 3) && check_divisible(n, 5)
    return "FizzBuzz"
  elsif check_divisible(n, 3)
    return "Fizz"
  elsif check_divisible(n, 5)
    return "Buzz"
  else
    return n.to_s
  end
end

# Loop through numbers from 1 to n and print the result
(1..n).each do |i|
  puts fizzbuzz(i)
end

All lessons in Fundamentals