์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ์—†์• ๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

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);
    }
}
  • ๋‹ค์‹œ ๋˜ ์ปดํŒŒ์ผ - ํ…Œ์ŠคํŠธ - ์ปค๋ฐ‹ํ•œ๋‹ค.
  • ๊ธด ํ•จ์ˆ˜๋ฅผ ์ž‘๊ฒŒ ์ชผ๊ฐœ๋Š” ๋ฆฌํŒฉํ„ฐ๋ง์€ ์ด๋ฆ„์„ ์ž˜ ์ง€์–ด์•ผ๋งŒ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค.
  • ๋‹จ๋ฒˆ์— ์ข‹์€ ์ด๋ฆ„์„ ์ง“๊ธฐ๋Š” ์‰ฝ์ง€ ์•Š๋‹ค. ๋‹น์žฅ์˜ ์ตœ์„ ์„ ์‚ฌ์šฉํ•˜๋‹ค ๋ฐ”๊พธ๋„๋ก ํ•˜์ž.

Reference