/
index.js
31 lines (26 loc) · 873 Bytes
/
index.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
/**
* @param {{name: string, quantity: number}[]} gifts
* @returns string
*/
function printTable(gifts) {
const maxNameLength = Math.max(...gifts.map(gift => gift.name.length), 4)
const maxQuantityLength = Math.max(
...gifts.map(gift => gift.quantity.toString().length),
8
)
const printRow = (name, quantity, padChar = ' ') =>
'| ' +
name.padEnd(maxNameLength, padChar) +
' | ' +
quantity.padEnd(maxQuantityLength, padChar) +
' |'
const top = '+'.repeat(maxNameLength + maxQuantityLength + 7)
const bottom = '*'.repeat(maxNameLength + maxQuantityLength + 7)
const header = printRow('Gift', 'Quantity')
const subheader = printRow('', '', '-')
const giftRows = gifts.map(gift =>
printRow(gift.name, gift.quantity.toString())
)
return [top, header, subheader, ...giftRows, bottom].join('\n')
}
export {printTable}