-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge-21.ts
31 lines (27 loc) · 884 Bytes
/
challenge-21.ts
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
export function printTable(gifts: Gift[]) {
const giftWidth = Math.max(...gifts.map(gift => gift.name.length), 4)
const quantityWidth = Math.max(
...gifts.map(gift => `${gift.quantity}`.length),
8,
)
const tableWidth = giftWidth + quantityWidth + 7
const top = '+'.repeat(tableWidth)
const bottom = '*'.repeat(tableWidth)
const head = `Gift${' '.repeat(giftWidth - 4)} | Quantity${' '.repeat(
quantityWidth - 8,
)}`
const border = `${'-'.repeat(giftWidth)} | ${'-'.repeat(quantityWidth)}`
const body = gifts
.map(
gift =>
`| ${gift.name}${' '.repeat(giftWidth - gift.name.length)} | ${
gift.quantity
}${' '.repeat(quantityWidth - `${gift.quantity}`.length)} |`,
)
.join('\n')
return `${top}\n| ${head} |\n| ${border} |\n${body}\n${bottom}`
}
export interface Gift {
name: string
quantity: number
}