์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

6.1. ํ•จ์ˆ˜ ์ถ”์ถœํ•˜๊ธฐ

๐Ÿœ๊ฐœ๋ฐœ์ž๋ผ๋ฉด/์ฑ…์„ ์ฝ์žโœ

by :ํ•ดํ”ผ๋ž˜๋น—๐Ÿพ 2021. 9. 26. 13:13

๋ณธ๋ฌธ

- ๋ชฉ์ /๊ตฌํ˜„์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค๊ธฐ 

highlight()ํ•จ์ˆ˜ ๋‚ด์— reverse()๋ผ๋Š” ๋ฉ”์„œ๋“œ๋งŒ ํ˜ธ์ถœ : ๊ธธ์ด๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Œ.


๋ฐฉ๋ฒ•

1. ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค๊ณ  ๋ชฉ์ ์„ ์ž˜ ๋“œ๋Ÿฌ๋‚ด๋Š” ์ด๋ฆ„์„ ๋ถ™์ธ๋‹ค. ('์–ด๋–ป๊ฒŒ'๊ฐ€ ์•„๋‹Œ '๋ฌด์—‡์„ ํ•˜๋Š”์ง€'๊ฐ€ ๋“œ๋Ÿฌ๋‚˜์•ผ ํ•œ๋‹ค)

-> ๋ชฉ์ ์ด ๋“œ๋Ÿฌ๋‚˜๋Š” ์ด๋ฆ„, ์ค‘์ฒฉ ํ•จ์ˆ˜๋ฅผ ์ง€์›ํ•˜๋Š” ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ถ”์ถœํ•œ ํ•จ์ˆ˜๋ฅผ ์›๋ž˜ ํ•จ์ˆ˜ ์•ˆ์— ์ค‘์ฒฉ, ์–ธ์ œ๋“  ํ•จ์ˆ˜ ์˜ฎ๊ธฐ๊ธฐ๋ฅผ ์ ์šฉ.

2. ์ถ”์ถœํ•  ์ฝ”๋“œ๋ฅผ ์›๋ณธ ํ•จ์ˆ˜์—์„œ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ ํ•จ์ˆ˜์— ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.

3. ์ถ”์ถœํ•œ ์ฝ”๋“œ ์ค‘ ์›๋ณธ ํ•จ์ˆ˜์˜ ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•˜๊ฑฐ๋‚˜ ์ถ”์ถœํ•œ ํ•จ์ˆ˜์˜ ์œ ํšจ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๋ณ€์ˆ˜๋Š” ์—†๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. ์žˆ๋‹ค๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค.

4. ๋ณ€์ˆ˜๋ฅผ ๋‹ค ์ฒ˜๋ฆฌํ–ˆ๋‹ค๋ฉด ์ปดํŒŒ์ผํ•œ๋‹ค.

5. ์›๋ณธ ํ•จ์ˆ˜์—์„œ ์ถ”์ถœํ•œ ์ฝ”๋“œ ๋ถ€๋ถ„์„ ์ƒˆ๋กœ ๋งŒ๋“  ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฌธ์žฅ์œผ๋กœ ๋ฐ”๊พผ๋‹ค. (์ฆ‰, ์ถ”์ถœํ•˜๋‚˜ ํ•จ์ˆ˜๋กœ ์ผ์„ ์œ„์ž„ํ•œ๋‹ค.)

6. ํ…Œ์ŠคํŠธํ•œ๋‹ค.

7. ๋‹ค๋ฅธ ์ฝ”๋“œ์— ๋ฐฉ๊ธˆ ์ถ”์ถœํ•œ๊ฒƒ๊ณผ ๋˜‘๊ฐ™๊ฑฐ๋‚˜ ๋น„์Šทํ•œ ์ฝ”๋“œ๊ฐ€ ์—†๋Š”์ง€ ์‚ดํ•€๋‹ค. ์žˆ๋‹ค๋ฉด ๋ฐฉ๊ธˆ์ถ”์ถœํ•œ ์ƒˆ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ๋ฐ”๊ฟ€์ง€ ๊ฒ€ํ† ํ•œ๋‹ค.

 

 


  ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ  

์ง€์—ญ ๋ณ€์ˆ˜๋“ค์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธฐ๋ฉด ๋œ๋‹ค.
// ๋ณ€๊ฒฝ ์ด์ „ ์ฝ”๋“œ
function printOwing(invoice){
    let outstanding=0;
    printBanner();
    for (const o of invoice.orders){
      outstanding+=o.amount;
    }
    const today = Clock.today;
    invoice.dueDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()+30);
    console.log(`๊ณ ๊ฐ๋ช… :${invoice.customer}`);
    console.log(`์ฑ„๋ฌด์•ก :${outstanding}`);
    console.log(`๋งˆ๊ฐ์ผ : ${invoice.dueDate.toLocaleDateString()}`);
}

// ๋ณ€๊ฒฝ ํ›„
function printOwing(invoice){

    let outstanding = 0;
    printBanner();
    for (const o of invoice.orders){
        outstanding+=o.amount;
    }
    // ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ผ๋ฉด ๋˜‘๊ฐ™์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธด ํ›„ ํ•„๋“œ ๊ฐ’์„ ์ˆ˜์ •
    recordDueDate(invoice);
    // ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ
    printDetails(invoice,outstanding);
    
}
function recordDueDate(invoice){
    const today = Clock.today;
    invoice.dueDate = new Date(today.getFullYear(), today.getMonth(), today.getDate()+30);
}
function printDetails(invoice,outstanding){
    console.log(`๊ณ ๊ฐ๋ช… :${invoice.customer}`);
    console.log(`์ฑ„๋ฌด์•ก :${outstanding}`);
    console.log(`๋งˆ๊ฐ์ผ : ${invoice.dueDate.toLocaleDateString()}`);
}

  ์ง€์—ญ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ๋•Œ  

๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด,
์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ์ƒˆ๋กœ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋„๋ก ํ•œ๋‹ค.
// 1) let outstanding = 0;์„ ๊ด€๋ จ ์žˆ๋Š” ์ฝ”๋“œ ๊ทผ์ฒ˜๋กœ ์˜ฎ๊ธด๋‹ค.
function printOwing(invoice){
    printBanner();
    
    let outstanding = 0;
    for (const o of invoice.orders){
        outstanding+=o.amount;
    }
    recordDueDate(invoice);
    printDetails(invoice,outstanding);
}

// 2) ์ถ”์ถœํ•  ์ฝ”๋“œ ๋ณต์‚ฌ
function printOwing(invoice){
    printBanner();
    
    let outstanding = 0;
    for (const o of invoice.orders){
        outstanding+=o.amount;
    }
    recordDueDate(invoice);
    printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
    let outstanding = 0; 
    for (const o of invoice.orders){
        outstanding += o.amount;
    }
    return outstanding; 
}

// 3) ์ƒˆ๋กœ์šด ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐ ๋ฐ˜ํ™˜๊ฐ’ ์ €์žฅ
function printOwing(invoice){
    printBanner();
    const outstanding = calculateOutstanding(invoice);
    recordDueDate(invoice);
    printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
    let outstanding = 0; 
    for (const o of invoice.orders){
        outstanding += o.amount;
    }
    return outstanding; 
}

// 4) ์ฝ”๋”ฉ์Šคํƒ€์ผ ๋ณ€๊ฒฝ
function printOwing(invoice){
    printBanner();
    const outstanding = calculateOutstanding(invoice);
    recordDueDate(invoice);
    printDetails(invoice,outstanding);
}
function calculateOutstanding(invoice){
    let result = 0; 
    for (const o of invoice.orders){
        result += o.amount;
    }
    return result; 
}

 ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด? 

๋ ˆ์ฝ”๋“œ๋กœ ๋ฌถ์–ด์„œ๋ฐ˜ํ™˜ : ์ž„์‹œ ๋ณ€์ˆ˜๋ฅผ ์งˆ์˜ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ๊ฑฐ๋‚˜, ๋ณ€์ˆ˜๋ฅผ ์ชผ๊ฐœ๋Š” ์‹์œผ๋กœ ์ฒ˜๋ฆฌ
์›๋ณธ ํ•จ์ˆ˜์™€ ๊ฐ™์€ ์ˆ˜์ค€์˜ ๋ฌธ๋งฅ์œผ๋กœ ๋จผ์ € ์ถ”์ถœํ•ด๋ณด์ž

 

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ