Im stuck on this. The challenge was to take a number and multiply it by its own digits and push that onto the array until you get two adjacent numbers in the array. I keep getting an Array cant be coerced into integer on the 2nd level of the block at line 10. No idea how digits is switching from an array to integer X amount of loops through the block.
If anyone could help me out id appreciate it a ton.
require 'byebug'
def ArrayChallenge(num)
# code goes here
return false if num.get_digits.adjacent_pair?
check = [pair(num.get_digits, 1)]
loop do
digits, multiplications = check.shift
digits.get_digits.each do |digit|
multi_digits = (digit * digits).get_digits
return multiplications if digits.last == multi_digits.first || multi_digits.adjacent_pair?
check.push(pair(multi_digits, multiplications + 1))
end
end
end
def pair(a, b)
a.hash < b.hash ? [a, b] : [b, a]
end
class Object
def get_digits
self.to_s.split('').map(&:to_i)
end
def get_num
self.to_i
end
def adjacent_pair?
digits = self
digits.each_with_index do |digit, index|
return true if digit == digits[index + 1]
end
false
end
end
puts ArrayChallenge(8)
Output:
➜ ~ ruby rubytest.rb
Traceback (most recent call last):
6: from rubytest.rb:40:in `<main>'
5: from rubytest.rb:7:in `ArrayChallenge'
4: from rubytest.rb:7:in `loop'
3: from rubytest.rb:9:in `block in ArrayChallenge'
2: from rubytest.rb:9:in `each'
1: from rubytest.rb:10:in `block (2 levels) in ArrayChallenge'
rubytest.rb:10:in `*': Array can't be coerced into Integer (TypeError)
If I put a puts before line 10 of digits.class I get this output:
➜ ~ ruby rubytest.rb
Integer
Traceback (most recent call last):
5: from rubytest.rb:41:in `<main>'
4: from rubytest.rb:7:in `ArrayChallenge'
3: from rubytest.rb:7:in `loop'
2: from rubytest.rb:9:in `block in ArrayChallenge'
1: from rubytest.rb:9:in `each'
rubytest.rb:12:in `block (2 levels) in ArrayChallenge': undefined method `last' for 1:Integer (NoMethodError)