loading...

re: Daily Challenge #241 - Tip Calculator VIEW POST

FULL DISCUSSION
 

Implementation in Frink

// Needs to be in a class as no concept of 'global' variables
class tipCalculator
{
  class var rates = new dict[[["terrible", 0 percent],
                              ["poor", 5 percent],
                              ["good", 10 percent],
                              ["great", 15 percent],
                              ["excellent", 20 percent]]]

  class calc[amount, rating] :=
  {
    r = lc[rating]
    if not rates.containsKey[r]
      return -1
    return ceil[amount * rates@r]
  }
}


println[tipCalculator.calc[30,"poor"]]          // 2
println[tipCalculator.calc[20, "hi"]]           // -1
println[tipCalculator.calc[107.65, "great"]]    // 17

println[tipCalculator.calc[78, "good"]]         // 8
println[tipCalculator.calc[50, "poor"]]         // 3
println[tipCalculator.calc[125, "excellent"]]   // 25
code of conduct - report abuse