-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildTower.js
47 lines (37 loc) · 1.29 KB
/
buildTower.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 'Build Tower' from https://www.codewars.com/kata/build-tower/train/javascript
/*Build Tower
Build Tower by the following given argument:
number of floors (integer and always greater than 0).
Tower block is represented as *
Python: return a list;
JavaScript: returns an Array;
C#: returns a string[];
PHP: returns an array;
C++: returns a vector<string>;
Haskell: returns a [String];
Have fun!
for example, a tower of 3 floors looks like below
[
' * ',
' *** ',
'*****'
]
and a tower of 6 floors looks like below
[
' * ',
' *** ',
' ***** ',
' ******* ',
' ********* ',
'***********'
]
Go challenge Build Tower Advanced (https://www.codewars.com/kata/57675f3dedc6f728ee000256) once you have finished this :)*/
const towerBuilder = n => Array.from({length: n}, (_, i, p = ' '.repeat(n - i - 1)) => p + '*'.repeat(i * 2 + 1) + p);
const expect = require('chai').expect;
describe('towerBuilder should make pyramid-shaped towers', () => {
it('should output the correct towers given 1, 2, and 3', () => {
expect(JSON.stringify(towerBuilder(1))).to.equal(JSON.stringify(['*']));
expect(JSON.stringify(towerBuilder(2))).to.equal(JSON.stringify([' * ', '***']));
expect(JSON.stringify(towerBuilder(3))).to.equal(JSON.stringify([' * ', ' *** ', '*****']));
});
});