Skip to content
loading...

re: Daily Challenge #241 - Tip Calculator VIEW POST

FULL DISCUSSION
 
 RATINGS = {
   terrible: 0,
   poor: 5,
   good: 10,
   great: 15,
   excellent: 20
 }.freeze

def calculatetip(bill, service)
  service = service.downcase.to_sym
  service_percentage = RATINGS[service]

  return 'Rating Not Recognized' unless service_percentage 

  tip = (bill.to_f * service_percentage.to_f / 100).ceil

  tip
end

calculatetip(78, "good") => 8
calculatetip(50, "poor") => 3
calculatetip(125, "excellent") => 25

 

Some minor suggestions:

  1. Move ratings out of the method, no need to recreate it.
  2. You can try to retrieve the service and do a nil check instead of checking keys, which seems to be more common in Ruby code in the wild.
  3. Floats are "viral", introducing one into the calculation makes all to_f calls redundant.
RATINGS = {
  terrible: 0,
  poor: 5,
  good: 10,
  great: 15,
  excellent: 20
}.freeze

def calculate_tip(bill, service)
  service = RATINGS[service.downcase.to_sym]
  return 'Rating Not Recognized' unless service

  (bill * service / 100.0).ceil
end
code of conduct - report abuse