-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build-Tower.js
41 lines (36 loc) · 959 Bytes
/
Build-Tower.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
//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];
//Ruby: returns an Array;
//Lua: returns a Table;
//Have fun!
function towerBuilder(nFloors) {
let tower = []
let element = ''
let spacer = ' '
let count = nFloors * nFloors
for(let i = 0; i <= count; i++ ){
if(element.length % 2 && tower.length + 1 <= nFloors){
tower.push(element)
}
element += '*'
}
for(let i = 0; i <= tower.length - 1; i++) {
if( i != tower.length - 1 ){
let floorArr = tower[i].split('')
for ( let j = nFloors - 1; j > i; j--){
floorArr.push(spacer)
floorArr.unshift(spacer)
}
tower[i] = floorArr.join('')
}
}
return tower
}