Answer
t.me/js_testОтвет:
function towerBuilder(nFloors) {
const result = [];
for (let i = 0; i < nFloors; i++) {
const str = "*".repeat(i * 2 + 1),
spaces = " ".repeat(nFloors - i - 1);
result.push(spaces + str + spaces);
}
return result;
}
Объяснение:
Создаём результирующий массив (result) который в конце вернём. Создаём цикл "по этажам" будущей пирамидки и высчитываем количество звёздочек (str) по формуле (N = i * 2 + 1) и количество пробелов слева и справа от звёздочек (spaces) по формуле (N = nFloors - i - 1), соединяем всё в одну строку и добавляем в массив.
Код для проверки:
function towerBuilder(nFloors) {
const result = [];
for (let i = 0; i < nFloors; i++) {
const str = "*".repeat(i * 2 + 1),
spaces = " ".repeat(nFloors - i - 1);
result.push(spaces + str + spaces);
}
return result;
}
console.log(towerBuilder(1));
console.log(towerBuilder(2));
console.log(towerBuilder(3));