I have been looking for a while for this answer and most results bring up people NOT wanting to push more than once, and its hard to find a straight forward solution / alternative.
I have been tasked with a exercise to output a tower in an array. It increments based on its floor size. For example.
[
' * ',
' *** ',
'*****'
]
I am still leaning the ropes of Javascript and I am intrigued to learn, here is what I have come up with:
function towerBuilder(floors) {
let arr = [];
let space = " ";
let tower = "*"
for (i = 0; i < floors; i++) {
arr.push(space, tower * i, space);
}
console.log(arr)
}
towerBuilder(3);
console.log shows (Array [ 0, NaN, 0, 0, NaN, 0, 0, NaN, 0 ]) - I guess multiplying a string is a bad idea.
I could probably create multiple for loops and have each push in its own loop, but that would look really messy.
Is there a way to push spaces and *'s multiplied by the for loop?
Thanks