-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6-1-printOwing.js
58 lines (49 loc) Β· 1.15 KB
/
6-1-printOwing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* p161 μμ
*/
function printOwing(invoice) {
printBanner();
const outstanding = calculateOutstanding(invoice);
recordDueDate(invoice);
printDetails(invoice, outstanding);
}
function printBanner() {
console.log('*******************');
console.log('**** κ³ κ° μ±λ¬΄ ****');
console.log('*******************');
}
function recordDueDate(invoice) {
const today = Clock.today;
invoice.dueDate = new Date(
today.getFullYear(),
today.getMonth(),
today.getDate() + 30
);
}
function calculateOutstanding(invoice) {
let result = 0; // <- λ³μ μ΄λ¦ λ³κ²½
for (const o of invoice.orders) {
result += o.amount;
}
return result;
}
function printDetails(invoice, outstanding) {
console.log(`κ³ κ°λͺ
: ${invoice.customer}`);
console.log(`μ±λ¬΄μ‘: ${outstanding}`);
console.log(`λ§κ°μΌ: ${invoice.dueDate.toLocaleDateString()}`);
}
/**
* μμ μ€νμ μν μμμ μ½λ
*/
const Clock = {
today: {
getFullYear: () => 2022,
getMonth: () => 11,
getDate: () => 10,
},
};
const invoice = {
customer: 'λ§ν΄ νμΈλ¬',
orders: [{ amount: 1000 }, { amount: 200 }],
};
printOwing(invoice);