Reputation: 1
I am doing some online exercises, and am stuck as the last test keeps failing.
The task involves creating a program to calculate the number of cars produced by a factory given the factory's speed. The rate per hour is equal to the speed of the factory (between 1-10) * 221. However, the speed effects the quality of the cars produced exponentially, so discounts are made depending on the speed, which were provided in the specification.
The code below is used to calculate how many whole and working cars can be produced per minute by a factory of a given speed between 1-10.
I have used the .floor method to ensure the answer is rounded down to the nearest whole number. All the tests are passing, except for when I enter 10. In this case, the code returns 27, not 28.
class AssemblyLine
def initialize(speed)
@speed = speed
end
def working_items_per_minute
rate = @speed * 221 / 60
if @speed <= 4
rate = rate
elsif @speed <= 8
rate = rate * 0.9
elsif @speed == 9
rate = rate * 0.8
else
rate = rate * 0.77
end
return rate.floor
end
end
Any ideas on why it is returning 27 would be appreciated! (221*10/60 *0.77 should be rounded down to 28!)
Upvotes: 0
Views: 47
Reputation: 71
The problem you're having is because ruby will deal in whole numbers until you introduce a Float.
It's easy to see what's going on if you launch a repl like irb or pry:
$ pry
pry(main)> 2210 / 60
=> 36
pry(main)> 2210 / 60.0
=> 36.833333333333336
pry(main)> 60.class
=> Integer
pry(main)> 60.0.class
=> Float
Upvotes: 1