-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcenter.js
More file actions
26 lines (21 loc) · 714 Bytes
/
center.js
File metadata and controls
26 lines (21 loc) · 714 Bytes
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
class Center {
constructor(str, width, fillchar = ' ') {
if (typeof str !== 'string') {
throw new Error('Input must be a string');
}
this.str = str;
this.width = width;
this.fillchar = fillchar;
}
execute() {
if (this.width <= this.str.length) {
return this.str;
}
const paddingLength = this.width - this.str.length;
const leftPadding = Math.floor(paddingLength / 2);
const rightPadding = Math.ceil(paddingLength / 2);
const paddedString = this.fillchar.repeat(leftPadding) + this.str + this.fillchar.repeat(rightPadding);
return paddedString;
}
}
module.exports = Center;