-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.js
153 lines (134 loc) · 4.45 KB
/
statement.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// var fs=require('fs');
// var dataI=fs.readFileSync('invoice.json', 'utf8');
// // var invoice=JSON.parse(dataI);
// let invoice = JSON.parse(fs.readFileSync('invoice.json').toString());
// // console.log(invoice);
// var dataP=fs.readFileSync('play.json');
// // var play=JSON.parse(dataP);
// var plays = JSON.parse(fs.readFileSync('play.json').toString());
// // console.log(play);
invoice = {
customer: 'BigCo',
performances: [{
"playID": "hamlet",
"audience": 55
},
{
"playID": "asLike",
"audience": 35
},
{
"playID": "othello",
"audience": 40
}
]
}
// console.log('invoice', invoice);
plays = {
hamlet: {
"name": "Hamlet",
"type": "tragedy"
},
asLike: {
"name": "As You Like It",
"type": "comedy"
},
othello: {
"name": "Othello",
"type": "tragedy"
}
}
// console.log('play', play);
// function statement() {
// // let invoice = JSON.parse(fs.readFileSync('invoice.json').toString());
// let totalAmount = 0;
// let volumeCredits = 0;
// console.log('in function statement invoice', invoice);
// console.log('in function statement play', plays);
// 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 += 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}`);
// }
// // add volume credits
// volumeCredits += Math.max(perf.audience - 30, 0);
// // add extra credit for every ten comedy attendees
// 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`;
// totalAmount += thisAmount;
// }
// result += `Amount owed is ${format(totalAmount/100)}\n`;
// result += `You earned ${volumeCredits} credits\n`;
// return result;
// }
// EXTRACTED FUNCTION
function amountFor(perf, play) {
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}`);
}
return thisAmount;
}
// New statment function
function statementTwo() {
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 (let perf of invoice.performances) {
const play = plays[perf.playID];
let thisAmount = amountFor(perf, play);
// add volume credits
volumeCredits += Math.max(perf.audience - 30, 0);
// add extra credit for every ten comedy attendees
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`;
totalAmount += thisAmount;
}
result += `Amount owed is ${format(totalAmount/100)}\n`;
result += `You earned ${volumeCredits} credits\n`;
return result;
}
// console.log('statement', statement());
console.log('statement', statementTwo());