I am working on a code challenge and have it mostly working, except it’s only going through the each.do iteration once. This is the iteration, calling on an array of 6 numbers. It prints one result, but additional results are expected.
@num.each do |x|
if amount >= x
qty = (amount / x ).to_i
puts "#{qty} * #{x} toys"
amount = (qty - x).to_i
end
end
Thank you for any insight!
You need to show all of your code. What are the values of @num, amount, qty, etc, coming into this loop?
You will get more results if you start with a large enough amount.
The first issue I see is amount isn’t defined the first time it’s called as part of the conditional. I assume it isn’t a method since the it does get set in the conditional, but if that is first place it gets set, then it won’t get set because it isn’t set when compared to x.
Also, if the (amount / x) - x is less than the next iteration value it would only run once.
ex. Given amount = 50 and @num = [5, 6,7,8,9,10]. qty would be 10 and amount is reset to 5. The next loop 5 is less than 6 so the conditional would be skipped from then on.