-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (79 loc) · 2.44 KB
/
script.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
function correctRandomColor() {
//Correct Hex
var correctRandomHex =
"#" + Math.floor(Math.random() * 16777215).toString(16);
//Incorrect Hexes
var incorrectRandomHex1 =
"#" + Math.floor(Math.random() * 16777215).toString(16);
var incorrectRandomHex2 =
"#" + Math.floor(Math.random() * 16777215).toString(16);
document.getElementById("hexCode").innerHTML = correctRandomHex;
let correctColor = Math.floor(Math.random() * 3);
if (correctColor === 1) {
assignColors(
"first",
"second",
"third",
correctRandomHex,
incorrectRandomHex1,
incorrectRandomHex2
);
} else if (correctColor == 2) {
assignColors(
"second",
"first",
"third",
correctRandomHex,
incorrectRandomHex1,
incorrectRandomHex2
);
} else {
assignColors(
"third",
"first",
"second",
correctRandomHex,
incorrectRandomHex1,
incorrectRandomHex2
);
}
}
function assignColors(
correct,
incorrect1,
incorrect2,
correctRandomHex,
incorrectRandomHex1,
incorrectRandomHex2
) {
document.getElementById(correct).style.backgroundColor = correctRandomHex;
document.getElementById(
incorrect1
).style.backgroundColor = incorrectRandomHex1;
document.getElementById(
incorrect2
).style.backgroundColor = incorrectRandomHex2;
document.getElementById(correct).addEventListener("click", function () {
document.getElementById("result").innerHTML="Voila, You guess is correct! Click restart for new game!";
x=document.getElementById("result");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
});
document.getElementById(incorrect1).addEventListener("click", function (e) {
document.getElementById("result").innerHTML="Aw, try again!";
x=document.getElementById("result");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 2500);
});
document.getElementById(incorrect2).addEventListener("click", function () {
document.getElementById("result").innerHTML="Aw, try again!";
x=document.getElementById("result");
x.className = "show";
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 2500);
});
}
correctRandomColor();
document.getElementById("restart").addEventListener("click", function () {
document.getElementById("result").innerHTML="";
correctRandomColor();
});