JavaScript: Tip Calculation: Math.ceil(), hash table

Using hash table and rounding up tip using Math.ceil

nick3499
1 min readJun 12, 2018
Chrome DevTools
  • n: has the restaurant tab integer assigned to it, e.g. n = 30.
  • s: has the service rating string assigned to it, e.g. s = “good”
  • tip: has the hash table assigned to it, containing key:value pairs which represent rating:tip factor

In the hash table, excellent: .20 represents an excellent service rating which deserves a 20% tip or tab * .20. The rating string is used to index the tip factor in the hash table, e.g. tip[s]. Math.ceil() rounds up the tip value.

If the tab is $30 and the service was excellent, the calculated tip is $6:

> tip
{ terrible: 0, poor: 0.05, good: 0.1, great: 0.15, excellent: 0.2 }
> Math.ceil(30 * tip["excellent"])
6

--

--