์ ๋ฆฌํ ํจ์๋ฅผ ๋ค์๋ณด์. ํธํจ์ ์ํด ์ค์ฒฉ์ํ๊ฐ ๋๋ฌด ๋ง์์ก๋ค.
์ค๊ฐ ์ ๊ฒ: ๋๋ฌดํ๋ ์ค์ฒฉํจ์
function statement(invoice, plays) {
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
for (let perf of invoice.performances) {
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${usd(amountFor(perf))} (${perf.audience}์)\n';
}
result += '์ด์ก: ${usd(totalAmount())}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${totalVolumeCredits())์ \n';
";
return result;
function amountFor(aPerformance) {
let result = 0;
switch (playFor(aPerformance).type) {
case "tragedy": // ๋น๊ทน
result = 40000;
if (aPerformance.audience > 30) {
result += 1000 * (aPerformance.audience - 30);
}
break;
case "comedy": // ํฌ๊ทน
result = 30000;
if (aPerformance.audience > 20) {
result += 10000 + 500 * (aPerformance.audience - 20);
}
result += 300 * aPerformance.audience;
break;
default:
throw new Error('์ ์ ์๋ ์ฅ๋ฅด: ${playFor(aPerformance).type}');
}
return result;080187
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function usd(aNumber) {
return new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format(aNumber / 100);
}
function totalVolumeCredits() {
let volumeCredits = 0;
for (let perf of invoice.performances) {
volumeCredits += volumeCreditsFor(perf);
}
return volumeCredits;
}
function totalAmount() {
let totalAmount = 0;
for (let perf of invoice.performances) {
totalAmount += amountFor(perf);
}
return totalAmount;
}
}
- ์ง๊ธ๊น์ง์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๊ณ์ฐ ๋ก์ง์ ๋ชจ๋ ๋นผ๋๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
- ํ์ง๋ง ์์ง ํจ์ ์์ ๋ค์ด๊ฐ ์๋ ์ค์ฒฉํจ์ ์ํ์ด๋ค.