Skip to content

Latest commit

 

History

History
56 lines (43 loc) · 1.15 KB

build-tower.md

File metadata and controls

56 lines (43 loc) · 1.15 KB

Build Tower 6 Kyu

LINK TO THE KATA - STRINGS ASCII ART FUNDAMENTALS

Description

Build a pyramid-shaped tower, as an array/list of strings, given a positive integer number of floors. A tower block is represented with "*" character.

For example, a tower with 3 floors looks like this:

[
  "  *  ",
  " *** ",
  "*****"
]

And a tower with 6 floors looks like this:

[
  "     *     ",
  "    ***    ",
  "   *****   ",
  "  *******  ",
  " ********* ",
  "***********"
]

Solution

const towerBuilder = nFloors => {
  const piramidBase = nFloors * 2 - 1
  let floorsArray = []

  for (let i = 1; i < nFloors + 1; i++) {
    const currentFloorBase = i * 2 - 1
    const bricks = '*'.repeat(currentFloorBase)
    const spaceDifference = (piramidBase - currentFloorBase) / 2
    const blankSpace = ' '.repeat(spaceDifference)

    const floorBuilt = `${blankSpace}${bricks}${blankSpace}`

    floorsArray.push(floorBuilt)
  }

  return floorsArray
}