-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtipCalculator.js
36 lines (30 loc) · 878 Bytes
/
tipCalculator.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
36
/*
Complete the function, 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 (so "great" = "GREAT"). If an unrecognised rating is received, then you need to return:
"Rating not recognised" in Javascript, Python and Ruby...
...or null in Java
...or -1 in C#
Because you're a nice person, you always round up the tip, regardless of the service.
*/
//Answer//
function calculateTip(a, r) {
switch(r.toLowerCase()){
case 'terrible': return 0;
break;
case 'poor': return Math.ceil(a/20);
break;
case 'good': return Math.ceil(a/10);
break;
case 'great': return Math.ceil(a*0.15);
break;
case 'excellent': return Math.ceil(a/5);
break;
default: return "Rating not recognised"
}
}