์ฐ๋ฆฌ๊ฐ ํ๊ณ ์ถ์๋ HTML์ ๋ง๋ค๊ธฐ ์ํด ๊ณ์ฐ ๋จ๊ณ์ ํฌ๋งทํ ๋จ๊ณ๋ฅผ ๋ถ๋ฆฌํ๋ ์์ ์ ์งํํด๋ณด์.
๊ณ์ฐ ๋จ๊ณ์ ํฌ๋งทํ ๋จ๊ณ ๋ถ๋ฆฌํ๊ธฐ
function statement(invoice, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customer})\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 totalAmount() {
let result = 0;
for (let perf of invoice.performances) {
result += amountFor(perf);
}
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(`unknown type: ${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);
}
func totalVolumeCredits() {
let result = 0;
for (let perf of invoice.performances) {
result += volumeCreditsFor(perf);
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
// add volume credits
result += Math.max(aPerformance.audience - 30, 0);
// add extra credit for every ten comedy attendees
if ("comedy" === playFor(aPerformance).type) result += Math.floor(aPerformance.audience / 5);
return result;
}
}- ์ง๊ธ๊น์ง ์งํํ ๊ณผ์ ์ ๊ตฌ์กฐ๋ฅผ ๋ณด๊ฐํ๋๋ฐ ์ฃผ์์ ์ ๋์๋ค.
- ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ธฐ ์ ์ ์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋ง ํ๊ณ ,
- ์ด์ ์คํ์ ์ถ๊ฐํด์ผ ํ๋ค.
statement()ํจ์์ ๊ฒฐ๊ณผ๋ฅผ HTML๋ก ๋ณ๊ฒฝํ๋ ๊ฒ์ ์ฒ๋ฆฌํด๋ณด์.- HTML ๋ฒ์ ์ ๋ง๋ ๋ค๊ณ ํด์
statement()ํจ์๋ฅผ ๋ณต์ฌํด์ ๊ตฌํ์ ๋ณ๊ฒฝํ ์๋ ์๋ค. - ๊ณ์ฐ์ ๋์ผํ๊ฒ ํ๋, ํํ๋ง ๋ณ๊ฒฝ๋๊ฒ ํด์ผ ํ๋ค.
- ์ด๋ฐ ๊ฒฝ์ฐ ๋จ๊ณ ์ชผ๊ฐ๊ธฐ๋ฅผ ์ฌ์ฉํ์. ํต์ฌ์ ์ค๊ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ ๋ค๋ฅธ ๋ฐฉ์์ผ๋ก ํํํ๋๋ก ํ๋ ๊ฒ์ด๋ค.
- ๋จ๊ณ ์ชผ๊ฐ๊ธฐ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ด๋๊น์ง๊ฐ ๊ณ์ฐ์ธ์ง๋ฅผ ์์๋ด์ด ์ถ์ถํด์ผ ํ๋ค. ์ด ๋จ๊ณ์์ ํจ์ ์ถ์ถํ๊ธฐ๋ฅผ ์ฌ์ฉํ๋ค.
- ์ ๊ฒฝ์ฐ์์๋
statement()ํจ์์ ๋ณธ๋ฌธ ์ ์ฒด๋ฅผ ์ถ์ถํ์.
function statement(invoice, plays) {
return renderPlainText(invoice, plays);
}
function renderPlainText(invoice, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customer})\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 statement(invoice, plays) {
const statementData = {};
return renderPlainText(statementData, invoice, plays);
}
function renderPlainText(data, invoice, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${invoice.customer})\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;
}- ์ด์
invoice,plays๋ฅผ ์ค๊ฐ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก ์ฎ๊ธฐ์. invoice๋ถํฐ ์ฎ๊ฒจ๋ณด์. ์ผ๋จ ํ์ํด๋ณด์ด๋ ๊ฑดcustomer๋ฟ์ด๋ค.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
return renderPlainText(statementData, invoice, plays);
}
function renderPlainText(data, invoice, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\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;
}- ๋ค์ ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ๊ทธ ๋ค์์ผ๋ก๋
performaces๋ฅผ ์ฎ๊ฒจ๋ณด์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer; // ๋ณ๊ฒฝ๋จ
statementData.performances = invoice.performances; // ๋ณ๊ฒฝ๋จ
return renderPlainText(statementData, invoice, plays);
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${playFor(perf).name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(totalAmount())}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${totalVolumeCredits()}์ \n";
return result;
...
function totalAmount() {
let result = 0;
for (let perf of data.performances) { // ๋ณ๊ฒฝ๋จ
result += amountFor(perf);
}
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
}- ๋ค์ ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
playFor(aPerformance)ํจ์๋ฅผ ๋ณด์.- ๊ฒฐ๊ตญ ํ๋ ํ์๋, ์ด๋ ํ ๊ณต์ฐ์ ๋ํ ์ฐ๊ทน ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ด๋ค.
- ๊ฐ์ ธ์จ ์ ๋ณด๋
result์ ์ถ๊ฐํ๋ค. - ๊ทธ๋ฐ๋ฐ ๊ตณ์ด ์ด๋ ๊ฒ ํ ํ์์์ด, ๋ฐ๊นฅ์ชฝ์์
data๋ฅผ ๋ฃ์ด์ฃผ๋ ์์ ์play์ ๋ณด๋ ์ ์ ์์ผ๋ฉด ์ข์ง ์์๊น? - ๋ณด๋ค ๋จ์ํ ๋ฐฉ์์ด๋ค.
- ์ฒ๋ฆฌํด๋ณด์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance); // ๋ณ๊ฒฝ๋จ
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance); // ์์ ๋ณต์ฌ ์ํ
return result;
}
}- ๋ค์ ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ์ด๋ ๊ฒ ํ๋ ์ด์ ๋ ํจ์์ ์ธ์๋ก ๋๊ธด ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ถ์ง ์๊ธฐ ๋๋ฌธ์ด๋ค.
- ์ด์
play์ ๋ณด๋ฅผ ๋ฃ์ด์ฃผ๋ฌ ๊ฐ๋ณด์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result); // play ์ ๋ณด๋ฅผ ์ค๊ฐ ๋ฐ์ดํฐ๋ก ์ฎ๊น
return result;
}
function playFor(aPerformance) { // renderPlainText()์ ์ค์ฒฉ ํจ์์๋ค๊ฐ statement()์ ํ์๋ก ์ฎ๊น
return plays[aPerformance.playID];
}
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n"; // play ์ ๋ณด๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
}
result += "์ด์ก: ${usd(totalAmount())}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${totalVolumeCredits()}์ \n";
return result;
function volumeCreditsFor(aPerformance) {
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === aPerformance.play.type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function totalAmount() {
let result = 0;
for (let perf of data.performances) {
result += amountFor(perf);
}
return result;
}
}- ๋ค์ ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
amountFor(aPerformance)์ญ์ ๊ณต์ฐ ์ ๋ณด์ ๋ก์ง์ด ๊ท์๋๋ฏ๋ก, ๊ทธ ์์ ์ ๋ณด๋ฅผ ๊ณ์ฐํด์ ๋ฃ์ด์ฃผ์.- ๋ณ๊ฒฝํด๋ณด์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result); // ์ค๊ฐ ๋ฐ์ดํฐ์ amount ์ ๋ณด๋ฅผ ์ ์ฅ
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n"; // play ์ ๋ณด๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
}
result += "์ด์ก: ${usd(totalAmount())}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${totalVolumeCredits()}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
function totalAmount() {
let result = 0;
for (let perf of data.performances) {
result += perf.amount; // ์ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
}
return result;
}
func totalVolumeCredits() {
let result = 0;
for (let perf of data.performances) { // ์ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
result += volumeCreditsFor(perf);
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
// add volume credits
result += Math.max(aPerformance.audience - 30, 0);
// add extra credit for every ten comedy attendees
if ("comedy" === playFor(aPerformance).type) result += Math.floor(aPerformance.audience / 5);
return result;
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ์ด์ ์ ๋ฆฝ ํฌ์ธํธ ๊ณ์ฐ ๋ถ๋ถ์ ์ฎ๊ธฐ์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result); // ์ค๊ฐ ๋ฐ์ดํฐ์ volumeCredits ์ ๋ณด๋ฅผ ์ ์ฅ
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function volumeCreditsFor(aPerformance) { // ์ ๋ฆฝ ํฌ์ธํธ ๊ณ์ฐ ๋ถ๋ถ์ ์ฎ๊น
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === playFor(aPerformance).type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(totalAmount())}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${totalVolumeCredits()}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
function totalAmount() {
let result = 0;
for (let perf of data.performances) {
result += perf.amount;
}
return result;
}
func totalVolumeCredits() {
let result = 0;
for (let perf of data.performances) {
result += perf.volumeCredits; // ์ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
}
return result;
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ๋ง์ง๋ง์ผ๋ก ์ดํฉ(์ ๋ฆฝ ํฌ์ธํธ, amount)์ ๊ตฌํ๋ ๋ถ๋ถ์ ์ฎ๊ธฐ์.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
statementData.totalAmount = totalAmount(); // ์ดํฉ์ ๊ตฌํ๋ ๋ถ๋ถ์ ์ฎ๊น
statementData.totalVolumeCredits = totalVolumeCredits(); // ์ดํฉ์ ๊ตฌํ๋ ๋ถ๋ถ์ ์ฎ๊น
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result); // ์ค๊ฐ ๋ฐ์ดํฐ์ volumeCredits ์ ๋ณด๋ฅผ ์ ์ฅ
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function volumeCreditsFor(aPerformance) { // ์ ๋ฆฝ ํฌ์ธํธ ๊ณ์ฐ ๋ถ๋ถ์ ์ฎ๊น
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === playFor(aPerformance).type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
function totalAmount(data) {
let result = 0;
for (let perf of data.performances) {
result += perf.amount;
}
return result;
}
func totalVolumeCredits(data) {
let result = 0;
for (let perf of data.performances) {
result += perf.volumeCredits; // ์ค๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋๋ก ๋ณ๊ฒฝ
}
return result;
}
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(data.totalAmount)}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${data.totalVolumeCredits}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ์ด๋ ๊ฒ๊น์ง ํ๋, ๋ฐ๋ณต๋ฌธ์ ํ์ดํ๋ผ์ธ์ผ๋ก ๋ฐ๊พธ๊ธฐ๊น์ง ์ ์ฉํ ์ ์๋ ์ํ๊ฐ ๋์๋ค.
function statement(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
statementData.totalAmount = totalAmount();
statementData.totalVolumeCredits = totalVolumeCredits();
return renderPlainText(statementData, plays);
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result);
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === playFor(aPerformance).type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
function totalAmount(data) {
return data.performances
.reduce((total, p) => total + p.amount, 0); // ๋ฐ๋ณต๋ฌธ์ ํ์ดํ๋ผ์ธ์ผ๋ก ๋ฐ๊พธ๊ธฐ
}
func totalVolumeCredits(data) {
data.performances
.reduce((total, p) => total + p.volumeCredits, 0); // ๋ฐ๋ณต๋ฌธ์ ํ์ดํ๋ผ์ธ์ผ๋ก ๋ฐ๊พธ๊ธฐ
}
}
function renderPlainText(data, plays) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(data.totalAmount)}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${data.totalVolumeCredits}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ๋ค์์ผ๋ก๋
statementData๋ฅผ ์์ฑํ๋ ๋ถ๋ถ์ ๋ถ๋ฆฌํ์.
function statement(invoice, plays) {
return renderPlainText(createStatementData(invoice, plays));
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result);
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === playFor(aPerformance).type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
function totalAmount(data) {
return data.performances
.reduce((total, p) => total + p.amount, 0);
}
func totalVolumeCredits(data) {
data.performances
.reduce((total, p) => total + p.volumeCredits, 0);
}
}
function createStatementData(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
statementData.totalAmount = totalAmount();
statementData.totalVolumeCredits = totalVolumeCredits();
return statementData;
}
function renderPlainText(data) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(data.totalAmount)}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${data.totalVolumeCredits}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ์ด์ ํ์ผ๊น์ง ๋ถ๋ฆฌํด๋ณด์.
// statement.js
import createStatementData from './createStatementData.js';
function statement(invoice, plays) {
return renderPlainText(createStatementData(invoice, plays));
}
function renderPlainText(data) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(data.totalAmount)}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${data.totalVolumeCredits}์ \n";
return result;
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}
}
// createStatementData.js
export default function createStatementData(invoice, plays) {
const statementData = {};
statementData.customer = invoice.customer;
statementData.performances = invoice.performances.map(enrichPerformance);
statementData.totalAmount = totalAmount();
statementData.totalVolumeCredits = totalVolumeCredits();
return statementData;
function enrichPerformance(aPerformance) {
const result = Object.assign({}, aPerformance);
result.play = playFor(result);
result.amount = amountFor(result);
result.volumeCredits = volumeCreditsFor(result);
return result;
}
function playFor(aPerformance) {
return plays[aPerformance.playID];
}
function amountFor(aPerformance) {
let result = 0;
switch (aPerformance.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('์ ์ ์๋ ์ฅ๋ฅด: ${aPerformance.play.type}');
}
return result;
}
function volumeCreditsFor(aPerformance) {
let result = 0;
result += Math.max(aPerformance.audience - 30, 0);
if ("comedy" === playFor(aPerformance).type)
result += Math.floor(aPerformance.audience / 5);
return result;
}
function totalAmount(data) {
return data.performances
.reduce((total, p) => total + p.amount, 0);
}
func totalVolumeCredits(data) {
data.performances
.reduce((total, p) => total + p.volumeCredits, 0);
}
}- ์ปดํ์ผ-ํ ์คํธ-์ปค๋ฐ์ ์ํํ๋ค.
- ์ด์ HTML ๋ฒ์ ์ ์์ฑํด๋ณด์.
// statement.js
import createStatementData from './createStatementData.js';
function statement(invoice, plays) {
return renderPlainText(createStatementData(invoice, plays));
}
function htmlStatement(invoice, plays) {
return renderHtml(createStatementData(invoice, plays));
}
function renderPlainText(data) {
let result = "์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})\n";
for (let perf of data.performances) {
result += " ${perf.play.name}: ${usd(amountFor(perf))} (${perf.audience}์)\n";
}
result += "์ด์ก: ${usd(data.totalAmount)}\n";
result += "์ ๋ฆฝ ํฌ์ธํธ: ${data.totalVolumeCredits}์ \n";
return result;
}
function renderHtml(data) {
let result = "<h1>์ฒญ๊ตฌ ๋ด์ญ (๊ณ ๊ฐ๋ช
: ${data.customer})</h1>\n";
result += "<table>\n";
result += "<tr><th>์ฐ๊ทน</th><th>์ข์ ์</th><th>๊ธ์ก</th></tr>";
for (let perf of data.performances) {
result += " <tr><td>${perf.play.name}</td><td>(${perf.audience}์)</td>";
result += "<td>${usd(amountFor(perf))}</td></tr>\n";
}
result += "</table>\n";
result += "<p>์ด์ก: <em>${usd(data.totalAmount)}</em></p>\n";
result += "<p>์ ๋ฆฝ ํฌ์ธํธ: <em>${data.totalVolumeCredits}</em>์ </p>\n";
return result;
}
function usd(aNumber) {
return new Intl.NumberFormat("en-US",
{ style: "currency", currency: "USD",
minimumFractionDigits: 2 }).format(aNumber/100);
}