-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor code
71 lines (62 loc) · 1.66 KB
/
refactor code
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function statement(invoice, plays)
{
let totalAmound = 0;
let volumeCredits = 0;
let result = `Statement for ${invoice.customer}\n`;
const format = new Intl.NumberFormat("en-US",
{style: "currency",currency: "USD",minimumFractionDigits:2}
).format;
for(let perf of invoice.performances) {
const play = plays[perf.playID];
let thisAmount = 0;
switch(play.type) {
case "tragedy":
thisAmount = 40000;
if (perf.audience > 30) {
thisAmount += 10000 * (perf.audience - 30);
}
break;
case "comedy":
thisAmount = 30000;
if (perf.audience > 20) {
thisAmount += 10000 + 500 *(perf.audience - 20);
}
thisAmount += 300 * perf.audience;
break;
default:
throw new Error(`unknown type: ${play.type}`);
}
//add volume credits
volumeCredits += Math.max(perf.audience - 30, 0);
if ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);
//print line for this order
result += `${play.name}: ${format(thisAmount/100)} (${perf.audience} seats)\n`;
totalAmound += thisAmount;
}
result += `Amount owed is ${format(totalAmound/100)}\n`;
result += `You earned ${volumeCredits} credits\n`;
return result;
}
const invoice = {
"customer": "BigCo",
"performances": [{
"playID": "hamlet",
"audience": 55
},
{
"playID": "as-like",
"audience": 35
},
{
"playID": "othello",
"audience": 40
}]
};
const plays = {
"hamlet" : {"name":"Hamlet","type":"tragedy"},
"as-like" : {"name":"As You Like It","type":"comedy"},
"othello" : {"name":"Othello","type":"tragedy"},
};
//console.log(invoice.performances);
const result = statement(invoice, plays);
console.log(result);