-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-21.ts
22 lines (19 loc) · 905 Bytes
/
day-21.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface Gift {
name: string
quantity: number
}
function printTable(gifts: Gift[]) {
const giftMap: number[] = gifts.map(el => el.name.length)
const quantityMap: number[] = gifts.map(el => String(el.quantity).length)
const gift: number = Math.max(...giftMap, "Gift".length)
const quantity: number = Math.max(...quantityMap, "Quantity".length)
const lineUp: string = "+".repeat(gift + quantity + 7)
const header: string = `| ${"Gift".padEnd(gift)} | ${"Quantity".padEnd(quantity)} |`
const lineHeader: string = `| ${"-".repeat(gift)} | ${"-".repeat(quantity)} |`
const contentMap: string[] = gifts.map((obj): Gift =>
`| ${obj.name.padEnd(gift)} | ${String(obj.quantity).padEnd(quantity)} |`
)
const lineDown: string = "*".repeat(gift + quantity + 7)
const content: string = contentMap.join("\n")
return `${lineUp}\n${header}\n${lineHeader}\n${content}\n${lineDown}`
}