1
+ var boxes = document . querySelectorAll ( ".box" ) ;
2
+ var s = document . querySelector ( "#rgbspan" ) ;
3
+ var colors = generateRandomColor ( 6 ) ;
4
+ var pickedColor = colors [ Math . floor ( Math . random ( ) * 6 ) ] ;
5
+ s . textContent = pickedColor ;
6
+ var playbtn = document . querySelector ( "#playAgain" ) ;
7
+ var easybtn = document . querySelector ( "#easyBtn" ) ;
8
+ var hardbtn = document . querySelector ( "#hardBtn" ) ;
9
+ var boxCount = 6 ;
10
+ var statusText = document . querySelector ( ".status" ) ;
11
+ statusText . textContent = "Lets Play!" ;
12
+
13
+ easybtn . addEventListener ( "click" , function ( ) {
14
+ document . querySelector ( "h1" ) . style . background = "#f88989" ;
15
+ statusText . textContent = "Lets Play!" ;
16
+ boxCount = 3 ;
17
+ this . style . background = "#f88989" ;
18
+ this . style . color = "white" ;
19
+ hardbtn . style . background = "white" ;
20
+ hardbtn . style . color = "rgb(233,119,119)" ;
21
+
22
+ colors = generateRandomColor ( boxCount ) ;
23
+ pickedColor = colors [ Math . floor ( Math . random ( ) * 3 ) ] ;
24
+ s . textContent = pickedColor ;
25
+
26
+ for ( var i = 0 ; i < boxes . length ; i ++ ) {
27
+ if ( colors [ i ] ) {
28
+ boxes [ i ] . style . background = colors [ i ] ;
29
+ } else {
30
+ boxes [ i ] . style . display = "none" ;
31
+ }
32
+ }
33
+
34
+ } ) ;
35
+ hardbtn . addEventListener ( "click" , function ( ) {
36
+ document . querySelector ( "h1" ) . style . background = "rgb(233,119,119)" ;
37
+ statusText . textContent = "Lets Play!" ;
38
+
39
+ this . style . background = "rgb(233,119,119)" ;
40
+ this . style . color = "white" ;
41
+ easybtn . style . background = "white" ;
42
+ easybtn . style . color = "rgb(233,119,119)" ;
43
+ boxCount = 6 ;
44
+
45
+ colors = generateRandomColor ( boxCount ) ;
46
+ pickedColor = colors [ Math . floor ( Math . random ( ) * 6 ) ] ;
47
+
48
+ for ( var i = 0 ; i < boxes . length ; i ++ ) {
49
+ boxes [ i ] . style . background = colors [ i ] ;
50
+ boxes [ i ] . style . display = "block" ;
51
+
52
+ }
53
+
54
+
55
+ } ) ;
56
+
57
+
58
+ playbtn . addEventListener ( "click" , function ( ) {
59
+ document . querySelector ( "h1" ) . style . background = "rgb(233,119,119)" ;
60
+ statusText . textContent = "Lets Play!" ;
61
+
62
+
63
+ colors = generateRandomColor ( boxCount ) ;
64
+ pickedColor = colors [ Math . floor ( Math . random ( ) * boxCount ) ] ;
65
+ s . textContent = pickedColor ;
66
+
67
+ for ( var i = 0 ; i < boxes . length ; i ++ ) {
68
+
69
+ boxes [ i ] . style . background = colors [ i ] ;
70
+
71
+
72
+ }
73
+ } ) ;
74
+ for ( var i = 0 ; i < colors . length ; i ++ ) {
75
+ boxes [ i ] . style . background = colors [ i ] ;
76
+ boxes [ i ] . addEventListener ( "click" , function ( ) {
77
+ var selectedcolor = this . style . background ;
78
+ if ( selectedcolor === pickedColor ) {
79
+ win ( ) ;
80
+ }
81
+ else {
82
+ loose ( this ) ;
83
+ }
84
+ } ) ;
85
+ }
86
+
87
+ function win ( ) {
88
+ for ( var i = 0 ; i < colors . length ; i ++ ) {
89
+ boxes [ i ] . style . background = pickedColor ;
90
+ }
91
+ document . querySelector ( "h1" ) . style . background = pickedColor ;
92
+ statusText . textContent = "CORRECT!!!" ;
93
+ }
94
+
95
+ function loose ( a ) {
96
+ a . style . background = "#2f2f2f" ;
97
+ statusText . textContent = "TRY AGAIN !" ;
98
+ }
99
+
100
+ function generateRandomColor ( num ) {
101
+ var arr = [ ] ;
102
+ for ( var i = 0 ; i < num ; i ++ ) {
103
+ arr . push ( randomColor ( ) ) ;
104
+ }
105
+ return arr ;
106
+ }
107
+
108
+ function randomColor ( ) {
109
+ var r = Math . floor ( Math . random ( ) * 256 ) ;
110
+ var g = Math . floor ( Math . random ( ) * 256 ) ;
111
+ var b = Math . floor ( Math . random ( ) * 256 ) ;
112
+ return "rgb(" + r + ", " + g + ", " + b + ")" ;
113
+ }
0 commit comments