-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.js
49 lines (42 loc) · 1000 Bytes
/
solution.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
const solve = (gifts) => {
const MaxGiftName = Math.max(
...gifts.map((gift) => gift.name.length),
4,
);
const MaxGiftQuantity = Math.max(
...gifts.map((gift) => `${gift.quantity}`.length),
8,
);
const MakeBlockBorder = (symbol) =>
symbol.repeat(MaxGiftName + MaxGiftQuantity + 7);
const topBorder = MakeBlockBorder('+');
const bottomBorder = MakeBlockBorder('*');
const boxHeader =
'| ' +
'Gift'.padEnd(MaxGiftName, ' ') +
' | ' +
'Quantity'.padEnd(MaxGiftQuantity, ' ') +
' |';
const boxSeparator =
'| ' +
'-'.repeat(MaxGiftName) +
' | ' +
'-'.repeat(MaxGiftQuantity) +
' |';
const boxContent = gifts.map(
(gift) =>
'| ' +
`${gift.name}`.padEnd(MaxGiftName, ' ') +
' | ' +
`${gift.quantity}`.padEnd(MaxGiftQuantity, ' ') +
' |',
);
return [
topBorder,
boxHeader,
boxSeparator,
...boxContent,
bottomBorder,
].join('\n');
};
module.exports = { solve };