์์ ๋ณ์๋ฅผ ์์ ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์.
statement ํจ์ ์ชผ๊ฐ๊ธฐ (๊ณ์)
play ๋ณ์ ์ ๊ฑฐํ๊ธฐ
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\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;
thisAmount = amountFor(perf, play);
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${play.name}: ${format(thisAmount / 100)} (${perf.audience}์)\n';
totalAmount += thisAmount;
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
func amountFor(aPerformance, play) {
let result = 0;
switch (play.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('์ ์ ์๋ ์ฅ๋ฅด: ${play.type}');
}
return result;
}
}
amountFor
ํจ์์ ์ธ์๋ฅผ ๋ณด๋ ๋์ค,play
๊ฐ ํ์์๋ค๋ ๊ฒ์ ์๊ฒ ๋์๋ค.- play๋
aPerformance
์์ ์ป์ ์ ์๋ค. - ์ด๋ฐ ๊ฒฝ์ฐ ์์ ๋ณ์๋ฅผ ์ง์ ํจ์๋ก ๋ฐ๊พธ๊ธฐ๋ฅผ ์ฌ์ฉํ๋ค.
- ์ฆ, ์์๋ก ๋ณ์๋ก ํ ๋นํด ๋๋ ๊ฒฝ์ฐ, โํด๋น ๊ฐ์ ๊ฐ์ ธ์!โ ๋ผ๋ ์๋์ ํจ์๋ก ๋ณ๊ฒฝํด์ ์ฒ๋ฆฌํ๋ค๋ ๋ง์ด๋ค.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
const play = playFor(perf);
let thisAmount = 0;
thisAmount = amountFor(perf, play);
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === play.type) volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${play.name}: ${format(thisAmount / 100)} (${perf.audience}์)\n';
totalAmount += thisAmount;
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
func amountFor(aPerformance, play) {
let result = 0;
switch (play.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('์ ์ ์๋ ์ฅ๋ฅด: ${play.type}');
}
return result;
}
func playFor(aPerformance) {
return plays[aPerformance.playID];
}
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ๊ทธ๋ฆฌ๊ณ โ๋ณ์ ์ธ๋ผ์ธ ํ๊ธฐโ๋ฅผ ์ ์ฉํ๋ค.
- ๊ตณ์ด ๋ณ์๋ก ๋นผ์ง ์๊ณ , ๋ฐ๋ก ํจ์ํธ์ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
play
โplayFor(perf)
๋ก ๋ณ๊ฒฝํ๋ค.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
let thisAmount = 0;
thisAmount = amountFor(perf, playFor(perf));
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type) volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(thisAmount / 100)} (${perf.audience}์)\n';
totalAmount += thisAmount;
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
func amountFor(aPerformance, play) {
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;
}
func playFor(aPerformance) {
return plays[aPerformance.playID];
}
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ์ผ๋จ ๋จ์ํ๊ฒ ํจ์๋ก ์ฃ๋ค ๋์ฒดํด๋ฒ๋ฆฐ๋ค.
- ์๊ฐํด๋ณด๋, ์์ ๋ณ์๋ฅผ ํจ์๋ก ๋์ฒดํจ์ผ๋ก์จ, ์ผ๋ฐํ๋๊ฒ ๋ค๋ฅธ ํจ์์์๋ ์ฌ์ฉํ ์ ์๊ฒ ๋ง๋ค์๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
- ์ฒ์์๋ ์ ๊ตณ์ด? ๋ผ ์๊ฐํ์ผ๋, ์ด ํ์๊ฐ ๊ฒฐ๊ตญ ํจ์์์ ์์กด์ฑ์ ์ ๊ฑฐํ๋ ํ๋์ด๋ผ๋ ๊ฒ์ ์ ์ ์์๋ค.
- ์ด๋ ๊ฒ ๋๊ณ ๋ณด๋,
amountFor
ํจ์์play
์ธ์๋ ๋ ์ด์ ํ์์๋ค๋ ๊ฒ์ ์ ์ ์๋ค. - ์ด๋ฅผ ์ญ์ ํด์ฃผ์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
let thisAmount = 0;
thisAmount = amountFor(perf);
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type) volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(thisAmount / 100)} (${perf.audience}์)\n';
totalAmount += thisAmount;
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
func 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;
}
func playFor(aPerformance) {
return plays[aPerformance.playID];
}
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ์, ์ด๋ ๊ฒ ๋๊ณ ์๊ฐํด๋ณด์. ๋๊ตฐ๊ฐ๋ ์ด๋ ๊ฒ ๋งํ ์ ์๋ค.
- โ์ด์ ์๋
statement
์์play
๋ฅผ ์ป์ด์amountFor
์๊ฒ ์ ๋ฌํ์๋๋ฐ, ์ด์ ๋amountFor
๊ฐplay
๋ฅผ ์ป์ด์ ์ฌ์ฉํ๊ณ ์๋ค.โ - โ๊ทธ๋ฌ๋ฉด์ ์กฐํ ๋น๋๊ฐ 3๋ฐฐ๋ ๋์๋ค. ์ด๊ฑด ์ฑ๋ฅ์ ๋ฌธ์ ๊ฐ ์์ ์ ์๋ค.โ
- ํ์ง๋ง ์ด๋ ๊ฒ ๋ณ๊ฒฝํด๋ ์ฑ๋ฅ์ ํฐ ์ํฅ์ ์๋ค. ์ฌ๊ฐํ๊ฒ ๋๋ ค์ง๋๋ผ๋ ์ ๋๋ก ๋ฆฌํฉํฐ๋ง ๋์๋ค๋ฉด ์ฑ๋ฅ ๊ฐ์ ์ด ํจ์ฌ ์์ํ๋ค.
- ๊ทธ๋ผ์๋ ์ด๋ ๊ฒ ์ง์ญ ๋ณ์๋ถํฐ ์ ๊ฑฐํ๋ ๊ฒ์ ์ถ์ถํ๊ธฐ๊ฐ ๋งค์ฐ ์ฌ์์ง๋ค๋๋ฐ์ ์๋ค.
- ์ถ์ถ ์์ ์ ์๋ ๊ฑฐ์ ํญ์ ์ง์ญ ๋ณ์๋ถํฐ ์ ๊ฑฐํ๋ค.
- ์ด์
amountFor
ํจ์์ ํ์ํ ์ธ์๋ฅผ ๋ชจ๋ ์ฒ๋ฆฌํ์ผ๋, ์ด๊ฑธ ํธ์ถํ๋ ์น๊ตฌ๋ฅผ ๋ณด์. thisAmount = amountFor(perf);
์ด๋ ๊ฒ ์ฌ์ฉํ๊ณ ์๊ณ , ์ด๊ฑด ๋จ์ํ ๊ฐ์ ๋ํ๊ธฐ ์ํด ์ฌ์ฉ์ค์ด๋ค.- ์ฆ, ์์๋ก ๋ณ์์ ํ ๋น ์ค์ด๋ค.
- ๊ทธ๋ผ ๋ญํด์ผ ํ๋ค? โ ๋ณ์ ์ธ๋ผ์ธ ํ๊ธฐ.
thisAmount
๋ณ์๋ฅผ ์ ๊ฑฐํ๊ณ ,amountFor
ํจ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๋ก ๋์ ํ์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type) volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(amountFor(perf) / 100)} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
}
์ ๋ฆฝ ํฌ์ธํธ ๊ณ์ฐ ์ฝ๋ ์ถ์ถํ๊ธฐ
- ์ผ๋จ ์ง๊ธ๊น์ง ๊ฒฐ๊ณผ๋ฅผ ๋ณด์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf.audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type)
volumeCredits += Math.floor(perf.audience / 5);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(amountFor(perf) / 100)} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
}
- ์ง์ญ ๋ณ์๋ฅผ ์์ค ํจ๊ณผ๊ฐ ๋ณด์ด๋๊ฐ?
- ์ง์ญ ๋ณ์๊ฐ ์๋ค๋ฉด, ํจ์๋ฅผ ์ถ์ถํ ๋ ์์ด ๊ณ ๋ คํด์ผํ๋ ๋์์ด ์ถ๊ฐ๋๋ ์ ์ด๋ค.
- ๊ทธ๋ฐ๋ฐ, ์ด๋ ๊ฒ ํด๋๋, ํจ์์ ๊ฒฐ๊ณผ๋ก ๋์ฒด๋์ด, ์ ๋ฆฝํฌ์ธํธ๋ฅผ ์ถ์ถํ๊ธฐ ๋ ํธํด์ก๋ค. (
volumeCredits
๊ณ์ฐ) - ์ผ๋จ ๊ฐ์ ๊ณ์ฐํ๋ ๋ก์ง์ ์ถ์ถํ๊ณ , ์ด ๊ฒฐ๊ณผ๋ฅผ ๋ํ๋๋ก ๋ณ๊ฒฝํด๋ณด์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
volumeCredits += volumeCreditsFor(perf);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(amountFor(perf) / 100)} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \n';
";
return result;
}
...
function volumeCreditsFor(perf) {
let volumeCredits = 0;
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
volumeCredits += Math.max(perf.audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type)
volumeCredits += Math.floor(perf.audience / 5);
return volumeCredits;
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ๋จ์ํ ์ฎ๊ธฐ๊ณ ๋์ํ๋,
volumeCreditsFor
ํจ์ ๋ด๋ถ ๋ณ์๋ฅผresult
๋ก ๊ณ ์น์.
function volumeCreditsFor(perf) {
let result = 0;
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
result += Math.max(perf.audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type)
result += Math.floor(perf.audience / 5);
return result;
}
Format ๋ณ์ ์ ๊ฑฐํ๊ธฐ
- ํ ์ํฉ์ ํ๋ฒ ๋ณด์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
const format = new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format;
for (let perf of invoice.performances) {
volumeCredits += volumeCreditsFor(perf);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(amountFor(perf) / 100)} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \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;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function volumeCreditsFor(perf) {
let result = 0;
//ํฌ์ธํธ๋ฅผ ์ ๋ฆฝํ๋ค.
result += Math.max(perf.audience - 30, 0);
//ํฌ๊ทน ๊ด๊ฐ 5๋ช
๋ง๋ค ์ถ๊ฐ ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ค.
if ("comedy" === playFor(perf).type)
result += Math.floor(perf.audience / 5);
return result;
}
}
- ์์ ๋ณ์๋ ์์ ์ด ์ํ ํจ์ ์์์๋ง ์๋ฏธ๊ฐ ์๊ธฐ ๋๋ฌธ์, ๊ธธ๊ณ ๋ณต์กํด์ ธ ๋ฌธ์ ๋ฅผ ์ผ์ผํค๊ธฐ ์ฝ๋ค.
- ๋ค์์ผ๋ก ๋ง๋งํ ๊ฑด ์ด์
format
์ด๋ค. - ์ด๊ฑธ ๋ถ๋ฆฌํ๊ณ ๋ชจ๋ ์ ์ฉํด๋ณด์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
for (let perf of invoice.performances) {
volumeCredits += volumeCreditsFor(perf);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${format(amountFor(perf) / 100)} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${format (totalAmount/100)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \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;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function format(aNumber) {
return new Intl.NumberFormat("en-Us", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2
}).format(aNumber);
}
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ๊ทธ๋ฐ๋ฐ ์ด๋ฆ์ด ๊ฑธ๋ฆฐ๋ค.
format
์ด๋ผ๋ ์ด๋ฆ์ ๋๋ฌด ์ผ๋ฐ์ ์ด๋ค. - ์ ์ ํ ์ด๋ฆ์ธ
usd
๋ก ๋ณ๊ฒฝํ์. ๋ํ ๋ณํ ๋ก์ง(/100
)๋usd
ํจ์ ์์ผ๋ก ์ฎ๊ธฐ์.
function statement(invoice, plays) {
let totalAmount = 0;
let volumeCredits = 0;
let result = '์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customers})\n';
for (let perf of invoice.performances) {
volumeCredits += volumeCreditsFor(perf);
//์ฒญ๊ตฌ ๋ด์ญ์ ์ถ๋ ฅํ๋ค.
result += '${playFor(perf).name}: ${usd(amountFor(perf))} (${perf.audience}์)\n';
totalAmount += amountFor(perf);
}
result += '์ด์ก: ${usd(totalAmount)}\n';
result += '์ ๋ฆฝ ํฌ์ธํธ: ${volumeCredits)์ \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;
}
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);
}
}
- ๋ค์ ๋ ์ปดํ์ผ - ํ ์คํธ - ์ปค๋ฐํ๋ค.
- ๊ธด ํจ์๋ฅผ ์๊ฒ ์ชผ๊ฐ๋ ๋ฆฌํฉํฐ๋ง์ ์ด๋ฆ์ ์ ์ง์ด์ผ๋ง ํจ๊ณผ๊ฐ ์๋ค.
- ๋จ๋ฒ์ ์ข์ ์ด๋ฆ์ ์ง๊ธฐ๋ ์ฝ์ง ์๋ค. ๋น์ฅ์ ์ต์ ์ ์ฌ์ฉํ๋ค ๋ฐ๊พธ๋๋ก ํ์.