1
+ var firstTurn = Math . random ( ) < 0.5
2
+
3
+ const startElm = document . getElementById ( 'start' )
4
+ const infoElm = document . getElementById ( 'info-container' )
5
+ const optionsElm = document . getElementById ( 'options' )
6
+ const resultElm = document . getElementById ( 'result' )
7
+ const history = document . getElementById ( 'guess-history' )
8
+ const option1 = document . getElementById ( 'option-1' )
9
+ const option2 = document . getElementById ( 'option-2' )
10
+
11
+
12
+ function start ( ) {
13
+ const responseNode = document . createElement ( 'span' )
14
+
15
+ startElm . classList . add ( 'hidden' )
16
+ infoElm . classList . add ( 'hidden' )
17
+ optionsElm . classList . remove ( 'hidden' )
18
+
19
+ responseNode . innerHTML = 1 ;
20
+ responseNode . classList . add ( 'float-left' ) ;
21
+
22
+ history . style . display = 'flex' ;
23
+
24
+ if ( firstTurn === 0 ) {
25
+ history . append ( responseNode )
26
+
27
+ option1 . innerHTML = 2 ;
28
+ option2 . innerHTML = 3 ;
29
+ }
30
+ }
31
+
32
+ function getOptimumValue ( value ) {
33
+ return Number ( value ) + Number ( 1 ) ;
34
+ }
35
+
36
+ function resetState ( ) {
37
+ const responseNode = document . createElement ( 'span' )
38
+ firstTurn = Math . random ( ) < 0.5 ;
39
+ setTimeout ( function ( ) {
40
+ history . innerHTML = '' ;
41
+ } , 500 )
42
+
43
+ if ( firstTurn === 0 ) {
44
+ responseNode . innerHTML = 1 ;
45
+ responseNode . classList . add ( 'float-left' ) ;
46
+ history . append ( responseNode )
47
+
48
+ option1 . innerHTML = 2 ;
49
+ option2 . innerHTML = 3 ;
50
+ } else {
51
+ option1 . innerHTML = 1 ;
52
+ option2 . innerHTML = 2 ;
53
+ }
54
+
55
+ setTimeout ( function ( ) {
56
+ resultElm . style . display = 'none' ;
57
+ } , 4000 )
58
+ }
59
+
60
+ function check ( e ) {
61
+ e . preventDefault ( ) ;
62
+
63
+ const textNode = document . createElement ( 'span' )
64
+ const responseNode = document . createElement ( 'span' )
65
+
66
+ const value1 = option1 . innerHTML ;
67
+ const value2 = option2 . innerHTML ;
68
+ let selected = 'option1'
69
+
70
+ const activeElement = document . activeElement ;
71
+ if ( activeElement . type === 'submit' ) {
72
+ selected = activeElement . name
73
+ }
74
+
75
+ if ( selected === 'option1' ) {
76
+ textNode . innerHTML = value1 ;
77
+ textNode . classList . add ( 'float-right' ) ;
78
+
79
+ let responseValue = getOptimumValue ( value1 ) ;
80
+ responseNode . innerHTML = responseValue ;
81
+ responseNode . classList . add ( 'float-left' ) ;
82
+
83
+ setTimeout ( function ( ) {
84
+ history . append ( textNode )
85
+ } , 100 ) ;
86
+
87
+ if ( Number ( value1 ) === 21 ) {
88
+ resultElm . style . display = 'flex' ;
89
+ resultElm . style . background = '#0088ff' ;
90
+ resultElm . innerHTML = 'YOU WON' ;
91
+ resetState ( ) ;
92
+ return false ;
93
+ } else if ( Number ( value1 ) > 21 ) {
94
+ resultElm . style . display = 'flex' ;
95
+ resultElm . style . background = 'red' ;
96
+ resultElm . innerHTML = 'YOU LOOSE' ;
97
+ resetState ( ) ;
98
+ return false ;
99
+ }
100
+
101
+ setTimeout ( function ( ) {
102
+ history . append ( responseNode )
103
+ } , 500 )
104
+
105
+ if ( Number ( responseValue ) === 21 ) {
106
+ resultElm . style . display = 'flex' ;
107
+ resultElm . style . background = 'red' ;
108
+ resultElm . innerHTML = 'YOU LOOSE' ;
109
+ resetState ( ) ;
110
+ return false ;
111
+ } else if ( Number ( responseValue ) > 21 ) {
112
+ resultElm . style . display = 'flex' ;
113
+ resultElm . style . background = '#0088ff' ;
114
+ resultElm . innerHTML = 'YOU WON' ;
115
+ resetState ( ) ;
116
+ return false ;
117
+ }
118
+
119
+ option1 . innerHTML = responseValue + 1 ;
120
+ option2 . innerHTML = responseValue + 2 ;
121
+ } else {
122
+ textNode . innerHTML = value2 ;
123
+ textNode . classList . add ( 'float-right' )
124
+
125
+ let responseValue = getOptimumValue ( value2 ) ;
126
+ responseNode . innerHTML = responseValue ;
127
+ responseNode . classList . add ( 'float-left' ) ;
128
+
129
+ setTimeout ( function ( ) {
130
+ history . append ( textNode )
131
+ } , 100 ) ;
132
+
133
+ if ( Number ( value2 ) === 21 ) {
134
+ resultElm . style . display = 'flex' ;
135
+ resultElm . style . background = '#0088ff' ;
136
+ resultElm . innerHTML = 'YOU WON' ;
137
+ resetState ( ) ;
138
+ return false ;
139
+ } else if ( Number ( value2 ) > 21 ) {
140
+ resultElm . style . display = 'flex' ;
141
+ resultElm . style . background = 'red' ;
142
+ resultElm . innerHTML = 'YOU LOOSE' ;
143
+ resetState ( ) ;
144
+ return false ;
145
+ }
146
+
147
+ setTimeout ( function ( ) {
148
+ history . append ( responseNode )
149
+ } , 500 )
150
+
151
+ if ( Number ( responseValue ) === 21 ) {
152
+ resultElm . style . display = 'flex' ;
153
+ resultElm . style . background = 'red' ;
154
+ resultElm . innerHTML = 'YOU LOOSE' ;
155
+ resetState ( ) ;
156
+ return false ;
157
+ } else if ( Number ( responseValue ) > 21 ) {
158
+ resultElm . style . display = 'flex' ;
159
+ resultElm . style . background = '#0088ff' ;
160
+ resultElm . innerHTML = 'YOU WON' ;
161
+ resetState ( ) ;
162
+ return false ;
163
+ }
164
+
165
+ option1 . innerHTML = responseValue + 1 ;
166
+ option2 . innerHTML = responseValue + 2 ;
167
+ }
168
+ setTimeout ( function ( ) {
169
+ history . scrollTop = history . scrollHeight ;
170
+ } , 500 )
171
+ }
0 commit comments