Skip to content

Commit f11c3a9

Browse files
author
juanantonioledesma
committed
Add "Build Tower" kata
1 parent 3fd4842 commit f11c3a9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

6-kyu/build-tower.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<h1>Build Tower <sup><sup>6 Kyu</sup></sup></h1>
2+
3+
<sup>
4+
<a href="https://www.codewars.com/kata/576757b1df89ecf5bd00073b">
5+
<strong>LINK TO THE KATA</strong>
6+
</a> - <code>STRINGS</code> <code>ASCII ART</code> <code>FUNDAMENTALS</code>
7+
</sup>
8+
9+
## Description
10+
11+
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.
12+
13+
For example, a tower with `3` floors looks like this:
14+
15+
```
16+
[
17+
" * ",
18+
" *** ",
19+
"*****"
20+
]
21+
```
22+
23+
And a tower with `6` floors looks like this:
24+
25+
```
26+
[
27+
" * ",
28+
" *** ",
29+
" ***** ",
30+
" ******* ",
31+
" ********* ",
32+
"***********"
33+
]
34+
```
35+
36+
## Solution
37+
38+
```javascript
39+
const towerBuilder = nFloors => {
40+
const piramidBase = nFloors * 2 - 1
41+
let floorsArray = []
42+
43+
for (let i = 1; i < nFloors + 1; i++) {
44+
const currentFloorBase = i * 2 - 1
45+
const bricks = '*'.repeat(currentFloorBase)
46+
const spaceDifference = (piramidBase - currentFloorBase) / 2
47+
const blankSpace = ' '.repeat(spaceDifference)
48+
49+
const floorBuilt = `${blankSpace}${bricks}${blankSpace}`
50+
51+
floorsArray.push(floorBuilt)
52+
}
53+
54+
return floorsArray
55+
}
56+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ JavaScript katas on Codewars are programming challenges that help you improve yo
2525
- **[Array.diff](./6-kyu/array-diff.md)**
2626
- **[Bit Counting](./6-kyu/bit-counting.md)**
2727
- **[Break camelCase](./6-kyu/break-camel-case.md)**
28+
- **[Build Tower!](./6-kyu/build-tower.md)**
2829
- **[Card game](./6-kyu/card-game.md)**
2930
- **[Checkered Board](./6-kyu/checkered-board.md)**
3031
- **[Convert string to camel case](./6-kyu/convert-string-to-camel-case.md)**

0 commit comments

Comments
 (0)