forked from constructorlabs/debugging-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtower.js
More file actions
44 lines (42 loc) · 1.13 KB
/
tower.js
File metadata and controls
44 lines (42 loc) · 1.13 KB
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
function onClick() {
if (inputsAreEmpty()) {
label.textContent = 'Error: input is empty.';
return;
}
updateLabel();
}
function inputsAreEmpty() {
if (getNumber1() === '' ) {
return true;
} else {
return false;
}
}
function updateLabel() {
var addend1 = getNumber1();
const starsOutput = towerBuilder(addend1);
label.textContent = "Your "+addend1+"* tower: " + starsOutput;
}
function getNumber1() {
return parseInt(inputs[0].value);
}
function towerBuilder(nFloors) {
let towerArr=[];
let stringLength=nFloors+(nFloors-1);
let starString="*";
let towerString=" ";
let stars=stringLength;
let spaces=0;
for (i=0; i<nFloors; i++){
towerString=towerString.repeat(spaces)+(starString.repeat(stars))+towerString.repeat(spaces);
towerArr.unshift(towerString);//add to array
stars-=2;
towerString=" ";
spaces= (stringLength-stars)/2;
}
return towerArr;
}
var inputs = document.querySelectorAll('input');
var label = document.querySelector('p');
var button = document.querySelector('button');
button.addEventListener('click', onClick);