1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-16 20:56:57 +02:00
CodeWars/6kyu/build_tower_advanced/solution.js
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

30 lines
716 B
JavaScript

String.prototype.centerJustify = function(length, char) {
let i = 0;
let str = this;
let toggle = true;
while (i + this.length < length) {
i++;
if (toggle)
str = str + char;
else
str = char + str;
toggle = !toggle;
}
return str;
}
function towerBuilder(nFloors, nBlockSz) {
let floor = '';
for (let i = 0; i < nBlockSz[0]; i++)
floor += '*';
const width = nFloors * 2 * nBlockSz[0] - nBlockSz[0];
let result = [];
for (let i = 0, count = 1; i < nFloors; i++, count += 2) {
for (let j = 0; j < nBlockSz[1]; j++) {
result.push(floor.centerJustify(width, ' '));
}
for (let j = 0; j < nBlockSz[0]; j++)
floor += '**';
}
return result;
}