towerBuilder Advanced

codewars 6 kyu

by trentHarlem

HTML

<div id='display'>
</div>

a tower of 6 floors with block size = (2, 1) looks like below<br>
[<br>
  '----------**----------', <br>
  '--------******--------', <br>
  '------**********------', <br>
  '----**************----', <br>
  '--******************--', <br>
  '**********************'<br>
]<br>'-' = spaces

<br><br><br>
[<br>
  '    **    ',<br>
  '    **    ',<br>
  '    **    ',<br>
  '  ******  ',<br>
  '  ******  ',<br>
  '  ******  ',<br>
  '**********',<br>
  '**********',<br>
  '**********'<br>
]<br>

CSS

body {
  font: 1.2em system-ui;
  text-align: center;
}

JavaScript

/* Build Tower Advanced

Build Tower by the following given arguments:
number of floors (integer and always greater than 0)
block size (width, height) 
*/

///
// second WORKING version //
function towerBuilder(floors, blockSize) {
  const [w, h] = blockSize // destructure array
  const tower = [];
  for (let i = 0; i < (floors); i++) {
  //change '-' to ' '
    const spaces = '-'.repeat(floors * w - i * w - w);
    //const spaces = ' '.repeat(floors * w - (i+1));//try
    const blockWidth = '*'.repeat(w * (i + i + 1)) // satisfies the block width
    tower.push(spaces + blockWidth + spaces)
    for (let j = 0; j < h - 1; j++) {
      tower.push(spaces + blockWidth + spaces) // satisfies the block height
    }
  }
  return tower
}

//// BROKEN MAP VERSION
/////////.   MAP        / ///////////////////
const towerBuilderMAP = (n, blockSize) => {
  const [w, h] = blockSize // destructure array
  const tower = [...Array(n)].map((_, i) => {
    const spaces = '-'.repeat(n * w - i * w - w);
    const blockWidth = '*'.repeat(w * (i + i + 1)); // block width
    // return spaces + blockWidth + spaces // works with h = 1
    if (h === 1) {
      return spaces + blockWidth + spaces
    } else {
      for (let j = 0; j < h ; j++) {
         (spaces + blockWidth + spaces) //satisfies block height
      }
    }
  }).join(`\n`)
  return tower
}
//for (let j = 0; j < h - 1; j++) {
//tower.push(spaces + blockWidth + spaces) // satisfies the block height
//' '.repeat(n - i - 1)+ '*'.repeat(i + i + 1) + ' '.repeat(n - i - 1)
//}) 
//}
//////////////////////////

// first WORKING version //
/* function towerBuilder(floors, blockSize) {
  const [w, h] = blockSize // destructure array
  const tower = [];
  for (let i = 0; i < (floors); i++) {
    const spaces = ' '.repeat(floors * w - i * w - w);
    const blockWidth = '*'.repeat(w) // satisfies the block width
    const floor = spaces + blockWidth.repeat(i + i + 1) + spaces
    tower.push(floor)
    for (let j = 0; j < h - 1; j++)...