-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchapter_1.js
49 lines (42 loc) · 1.31 KB
/
chapter_1.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
37
38
39
40
41
42
43
44
45
46
47
48
49
function statement(invoice, plays) {
let totalAmount = 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 (const perf of invoice.performances) {
const play = plays[perf.playID]
let thisAmount = 0
switch (play.type) {
case 'tragedy':
thisAmount = 40000
if (perf.audience > 30) {
thisAmount += 1000 * (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}`)
}
// 加入 volume credit
volumeCredits += Math.max(perf.audience - 30, 0)
// 每十名喜劇觀眾可獲得額外分數
if (play.type === 'comedy') volumeCredits += Math.floor(perf.audience / 5)
// 印出這筆訂單
result += `${play.name}: ${format(thisAmount / 100)} (${perf.audience} seats)\n`
totalAmount += thisAmount
}
result += `Amount owed is ${format(totalAmount / 100)}\n`
result += `You earned ${volumeCredits} credits\n`
return result
}
module.exports = { statement }