-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-framingNames.js
51 lines (40 loc) · 950 Bytes
/
02-framingNames.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
// Challenge #2: 🖼️ Framing Names.
/**
* @param {string[]} names - Array of names to frame
* @returns {string} The framed names
*/
function createFrame(names) {
const width = Math.max(...names.map((name) => name.length)) + 4;
const frame = ["*".repeat(width)];
names.forEach((name) => {
frame.push(`* ${name.padEnd(width - 4, " ")} *`);
});
frame.push("*".repeat(width));
return frame.join("\n");
}
console.log(createFrame(["midu", "madeval", "educalvolpz"]));
// Expected result:
// ***************
// * midu *
// * madeval *
// * educalvolpz *
// ***************
console.log(createFrame(["midu"]));
// Expected result:
// ********
// * midu *
// ********
console.log(createFrame(["a", "bb", "ccc"]));
// Expected result:
// *******
// * a *
// * bb *
// * ccc *
// *******
console.log(createFrame(["a", "bb", "ccc", "dddd"]));
// ********
// * a *
// * bb *
// * ccc *
// * dddd *
// ********