I am trying to create several strings comprising of varying amounts of whitespaces only. I have been able to do this once with array.(number of indexes).join(" ");
However when I go into a loop to add more strings with an incrementing amount of white space in each string, the string is set to 'undefined.' I am not sure where I am going wrong. The function seems to work outside of the loop but not inside. Any ideas what I have done wrong?
function towerBuilder(nFloors) {
// build here
var towers = [];
var stars = "*";
var spaceNo = nFloors -1;
let spaces = Array(spaceNo).join(" ");
spaceNo -= 1;
towers[0] = spaces + stars + spaces;
for(i = 1; i <= nFloors -1; i++)
{
stars = stars + Array(i + 2).join('*');
let edges = Array(spaceNo).join(" ");
towers[i] = edges + stars + edges;
spaceNo -= 1;
}
return towers;
}
console.log(towerBuilder(3));
At the top, this works:
let spaces = Array(spaceNo).join(" ");
However, down in the loop, this outputs 'undefined'
let edges = Array(spaceNo).join(" ");
The idea is that the spaces wrap the star symbols so that a tower can be built out of an array. Example, calling towerBuilder(3) would output the below: (I don't need to output it with carriage returns. The array alone will suffice.)
[
' * ',
' *** ',
'*****'
]
undefined. Your question says "this outputs 'undefined'". It doesn't.