-
Notifications
You must be signed in to change notification settings - Fork 4
/
073-tip-calculator.js
35 lines (30 loc) · 990 Bytes
/
073-tip-calculator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Write a function, calculateTip(amount, rating) which calculates how much
// you need to tip based on the total amount of the bill and the service.
// You need to consider the following ratings:
// Terrible: tip 0%
// Poor: tip 5%
// Good: tip 10%
// Great: tip 15%
// Excellent: tip 20%
// The rating is case insensitive. If an unrecognised rating is input, then
// you need to return:
// "Rating not recognised" in Javascript, Python and Ruby...
// ...or null in Java
// Because you're a nice person, you always round up the tip, regardless of
// the service.
function calculateTip(amount, rating) {
switch (rating.toLowerCase()) {
case 'terrible':
return Math.ceil(amount * 0);
case 'poor':
return Math.ceil(amount * 0.05);
case 'good':
return Math.ceil(amount * 0.1);
case 'great':
return Math.ceil(amount * 0.15);
case 'excellent':
return Math.ceil(amount * 0.2);
default:
return "Rating not recognised";
}
}