0

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.)

[
  '  *  ', 
  ' *** ', 
  '*****'
]
3
  • 1
    "owever, down in the loop, this outputs 'undefined'" Not in the code you've posted. I took it and dropped it into a Stack Snippet, and it doesn't do what you describe. Please update your question with a minimal reproducible example demonstrating the problem (ideally using Stack Snippets as I did; here's more info about them). Commented Dec 4, 2017 at 9:04
  • What output are you getting? I am expecting to see spaces wrapped around the stars on each line but I only get the below. [ " * ", "***", "******" ] Commented Dec 4, 2017 at 9:32
  • Which isn't undefined. Your question says "this outputs 'undefined'". It doesn't. Commented Dec 4, 2017 at 9:34

1 Answer 1

1

Array.join is used to concatenate all the array's values, with the given string between them:

['foo','bar'].join(',') // = 'foo,bar'

You are expecting Array(2).join(' ') to be two spaces, but it will be only one since it's concatenating two empty values with a space in between.

Just remove the -1 at row 5 and it will work as expected:

function towerBuilder(nFloors) {
    // build here
    var towers = [];
    var stars = "*";
    var spaceNo = nFloors; // was 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));

Btw, it would look a lot better if written like this:

function towerBuilder(nFloors) {
    // build here
    var towers = [];
    
    for(var floor = 0; floor < nFloors; floor++)
    {
      var nStars = (floor * 2) + 1;
      var stars = Array(nStars + 1).join('*');
      var edges = Array(nFloors - floor).join(" ");
      towers[floor] = edges + stars + edges;
    }

    return towers;
}
console.log(towerBuilder(3));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.