const character = "#";
const count = 8;
const rows = [];
function padRow(rowNumber, rowCount) {
return " ".repeat(rowCount - rowNumber) + character.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}
let done = 0;
while (rows.length < count) {
done++;
rows.push(padRow(done, count));
}
let result = ""
for (const row of rows) {
result = result + row + "\n";
}
console.log(result);
These are the instructions for this step:
Using
done
to track the number of rows that have been generated is functional, but you can actually clean up the logic a bit further.
Arrays have a speciallength
property that allows you to see how many values, or elements, are in the array. You would access this property using syntax likemyArray.length
.
Note thatrows.length
in thepadRow
call would give you an off-by-one error, becausedone
is incremented before the call.
Update your condition to check ifrows.length
is less thancount
.
Could someone explain what this means? why would it be off-by-one?
Note that
rows.length
in thepadRow
call would give you an off-by-one error, becausedone
is incremented before the call