DIGIT = { A: 1, B: 2, F: 6 }
scores1 = [ :A, :A, :A, :A, :A ]
scores2 = [ :B, :B, :A, :A, :B ]
scores3 = [ :F, :B, :F, :F, :A ]
def verdict(scores)
scores.map! { |score| DIGIT[score] }
average = scores.sum / scores.length
if average == 1
puts 'Aced!'
elsif average <= 2
puts 'Ok.'
else
puts 'Uh! Oh!'
end
end
verdict(scores1) # Aced!
verdict(scores2) # Aced!
verdict(scores3) # Uh! Oh!
Why does scores2 count as aced?
1 Response
In order to fix it, instead of Integer division we have to perform Float division. We can execute that by calling Ruby `Float.to_f` method when performing division.
```ruby
average = scores.sum.to_f / scores.length.to_f # 8.0 / 5.0 = 1.6
```
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.