-
Notifications
You must be signed in to change notification settings - Fork 0
/
script-demo.js
119 lines (101 loc) · 3.14 KB
/
script-demo.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// --- DEKLARASI ---
let fieldContainer = document.querySelector('.field-container');
let maxRocket = 4;
let maxShip = 8;
let jmlRocket = document.getElementById('jumlahRocket');
let fieldColoumn = 25;
let lokasiRocket = [];
const btnLauchNow = document.getElementById('launch');
for (i = 0; i < fieldColoumn; i++) {
let elementField = document.createElement('li');
let fieldText = document.createTextNode('x');
elementField.appendChild(fieldText);
elementField.classList.add('field');
fieldContainer.appendChild(elementField);
}
let field = document.querySelectorAll('.field');
console.log(fieldContainer);
field.forEach((e, i) => {
e.setAttribute('id', i + 1);
});
//
// --- HOVER ACTION ---
const fieldHover = (e) => {
if (e.target.className == 'field') {
for (i = 0; i < field.length; i++) {
if (field[i].className == 'field plus') {
if (field[i].children[0] != undefined) {
if (field[i].children[0].className == 'field rocket') {
}
} else {
field[i].innerHTML = 'x';
field[i].style.fontSize = '15px';
field[i].className = 'field';
}
}
}
e.target.innerHTML = '+';
e.target.style.fontSize = '25px';
e.target.className = 'field plus';
}
if (e.target.className == 'field-container') {
for (i = 0; i < field.length; i++) {
if (field[i].className == 'field plus') {
if (field[i].children[0] != undefined) {
if (field[i].children[0].className == 'field rocket') {
}
} else {
field[i].innerHTML = 'x';
field[i].style.fontSize = '15px';
field[i].className = 'field';
}
}
}
}
};
//
// --- CLICK ACTION ---
const clickit = (e) => {
if (e.target.className == 'field plus') {
e.target.innerHTML = '';
const fieldRocket = document.createElement('li');
const contentRocket = document.createTextNode('');
fieldRocket.appendChild(contentRocket);
fieldRocket.className = 'field rocket';
e.target.appendChild(fieldRocket);
e.target.style.display = e.target.removeEventListener(
'mouseover',
fieldHover
);
}
const sisaRocket =
maxRocket - document.getElementsByClassName('rocket').length;
console.log(sisaRocket);
jmlRocket.innerHTML = '= ' + sisaRocket;
if (sisaRocket == 0) {
fieldContainer.removeEventListener('click', clickit);
}
const rocketGroup = document.querySelectorAll('.rocket');
rocketGroup.forEach((e) => {
e.parentElement.classList.add('fieldAfter');
});
};
//
// --- LAUNCH NOW ACTION ---
const funcLaunchNow = (e) => {
const rocketGroup = document.querySelectorAll('.rocket');
console.log(rocketGroup);
rocketGroup.forEach((e) => {
e.classList.add('rocketLaunch');
});
rocketGroup.forEach((e, i) => {
lokasiRocket[i] = e.parentElement.getAttribute('id');
});
};
//
// --- A BUNCH OF EVENT LISTENER
fieldContainer.addEventListener('mouseover', fieldHover);
fieldContainer.addEventListener('click', clickit);
btnLauchNow.addEventListener('click', funcLaunchNow);
// const letakRocket = document.getElementsByClassName('rocket').length;
// const usedRocket = letakRocket.length;