I am trying to complete a coding exercise in Ruby, which is as follows:
TODO: starting with an array of integers, return an array with integers and 'Fizz', 'Buzz' or 'FizzBuzz'
Write a method
fizz_buzz
, which takes anumber
as an argument, and return an array ofnumber
elements from 1 tonumber
, but replaces some of them according to these rules:
- If the number is divisible by
3
, then replace it by'Fizz'
- If the number is divisible by
5
, then replace it by'Buzz'
- If the number is divisible by both
3
and5
, then replace it by'FizzBuzz'
Write
If I follow a rubocop style guide
, which we have been asked to use after completed the task, and, as instructed, use .zero
instead of == 0?
my method fails and I don't get why.
My now failing solution with the edits required by my style guide:
def fizz_buzz(number)
fail ArgumentError, "#{number} is less than 1" if number < 1
a = [number]
while number > 1
number = number - 1
a.unshift(number)
a.map! { |x| (x % 15).zero? ? 'FizzBuzz' : x }
a.map! { |x| (x % 3).zero? ? 'Fizz' : x }
a.map! { |x| (x % 5).zero? ? 'Buzz' : x }
end
a
end
should return the array [ 1, 2, 'Fizz' ] for number 3 (FAILED - 1)
should return the array [ 1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7 ] for number 7 (FAILED - 2)
should return an array with 'FizzBuzz' at the 15th element of the array (15 is divisible by both 3 and 5) (FAILED - 3)
Failures:
1) fizz_buzz should return the array [ 1, 2, 'Fizz' ] for number 3
Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }
NoMethodError:
undefined method `zero?' for "Fizz":String
# ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
# ./lib/fizz_buzz.rb:12:in `map!'
# ./lib/fizz_buzz.rb:12:in `fizz_buzz'
# ./spec/fizz_buzz_spec.rb:13:in `block (2 levels) in <top (required)>'
2) fizz_buzz should return the array [ 1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7 ] for number 7
Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }
NoMethodError:
undefined method `zero?' for "Fizz":String
# ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
# ./lib/fizz_buzz.rb:12:in `map!'
# ./lib/fizz_buzz.rb:12:in `fizz_buzz'
# ./spec/fizz_buzz_spec.rb:17:in `block (2 levels) in <top (required)>'
3) fizz_buzz should return an array with 'FizzBuzz' at the 15th element of the array (15 is divisible by both 3 and 5)
Failure/Error: a.map! { |x| (x % 5).zero? ? 'Buzz' : x }
NoMethodError:
undefined method `zero?' for "Fizz":String
# ./lib/fizz_buzz.rb:12:in `block in fizz_buzz'
# ./lib/fizz_buzz.rb:12:in `map!'
# ./lib/fizz_buzz.rb:12:in `fizz_buzz'
# ./spec/fizz_buzz_spec.rb:21:in `block (2 levels) in <top (required)>'
My working solution which fails the style guide:
def fizz_buzz(number)
fail ArgumentError, "#{number} is less than 1" if number < 1
a = [number]
while number > 1
number = number - 1
a.unshift(number)
a.map! { |x| (x % 15) == 0 ? 'FizzBuzz' : x }
a.map! { |x| (x % 3) == 0 ? 'Fizz' : x }
a.map! { |x| (x % 5) == 0 ? 'Buzz' : x }
end
a
end
Solution supplied with the task assigned:
# def fizz_buzz(number)
# fail ArgumentError, "#{number} should be greater than 1" if number < 1
# (1..number).map do |i|
# if (i % 3).zero? && (i % 5).zero?
# 'FizzBuzz'
# elsif (i % 3).zero?
# 'Fizz'
# elsif (i % 5).zero?
# 'Buzz'
# else
# i
# end
# end
# end