-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (43 loc) · 1.67 KB
/
script.js
File metadata and controls
43 lines (43 loc) · 1.67 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
let boxes = document.querySelectorAll('.box');
let popup = document.querySelector('.popup');
let winner = document.querySelector('.winner');
let cross = document.querySelector('.cross');
cross.addEventListener('click',()=>{
location.reload();
})
let turn = "O";
function changeturn() {
if (turn == "O") {
turn = "X";
} else {
turn = "O";
}
return turn;
}
let probab = ['0,1,2', '3,4,5', '6,7,8', '0,4,8', '2,4,6', '0,3,6', '1,4,7', '2,5,8'];
let clickedindex = 0;
boxes.forEach((element, index) => {
element.addEventListener('click', () => {
element.innerHTML = changeturn();
clickedindex++;
probab.forEach(element => {
let newarray = [];
newarray = element.split(',');
if (boxes[newarray[0]].innerHTML != "" && boxes[newarray[1]].innerHTML != "" && boxes[newarray[2]].innerHTML != "") {
if (boxes[newarray[0]].innerHTML == boxes[newarray[1]].innerHTML && boxes[newarray[0]].innerHTML == boxes[newarray[2]].innerHTML) {
popup.style.transform = `scale(1)`;
popup.style.transitionDuration =`0.3s`;
winner.innerHTML = turn + ` Won`;
}else if(clickedindex==9 && popup.style.transform!=`scale(1)`){
popup.style.transform = `scale(1)`;
popup.style.transitionDuration =`0.3s`;
winner.innerHTML = `Draw`;
}
}
})
if (element.innerHTML == "X") {
element.style.backgroundColor = "#FB2576";
element.style.color = "white";
}
}, { once: true });
})