์ •๋ฆฌํ•œ ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ๋ณด์ž. ํŽธํ•จ์„ ์œ„ํ•ด ์ค‘์ฒฉ์ƒํƒœ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„์กŒ๋‹ค.

์ค‘๊ฐ„ ์ ๊ฒ€: ๋‚œ๋ฌดํ•˜๋Š” ์ค‘์ฒฉํ•จ์ˆ˜

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;
    }
}
 
  • ์ง€๊ธˆ๊นŒ์ง€์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๊ณ„์‚ฐ ๋กœ์ง์„ ๋ชจ๋‘ ๋นผ๋ƒˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์•„์ง ํ•จ์ˆ˜ ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์ค‘์ฒฉํ•จ์ˆ˜ ์ƒํƒœ์ด๋‹ค.

Reference