1+ var timeremaining ;
2+ var correctAnswer ;
3+ var score ;
4+ var action ;
5+ var playing = false ;
6+
7+ document . getElementById ( "startreset" ) . onclick = function ( ) {
8+ if ( playing == true ) {
9+ location . reload ( ) ;
10+ }
11+ else {
12+ playing = true ;
13+ score = 0 ;
14+
15+ document . getElementById ( "scorevalue" ) . innerHTML = score ;
16+
17+ show ( "timeremaining" ) ;
18+ timeremaining = 60 ;
19+
20+ document . getElementById ( "timeremainingvalue" ) . innerHTML = timeremaining ;
21+
22+ hide ( "gameOver" ) ;
23+
24+ document . getElementById ( "startreset" ) . innerHTML = "Reset Game" ;
25+
26+ startCountdown ( ) ;
27+
28+ generateQA ( ) ;
29+
30+ }
31+ }
32+
33+ for ( i = 1 ; i < 5 ; i ++ ) {
34+ document . getElementById ( "box" + i ) . onclick = function ( ) {
35+ if ( playing == true ) {
36+ if ( this . innerHTML == correctAnswer ) {
37+
38+ score ++ ;
39+ document . getElementById ( "scorevalue" ) . innerHTML = score ;
40+ hide ( "wrong" ) ;
41+ show ( "correct" ) ;
42+ setTimeout ( function ( ) {
43+ hide ( "correct" ) ;
44+ } , 1000 ) ;
45+ generateQA ( ) ;
46+
47+ } else {
48+
49+ hide ( "correct" ) ;
50+ show ( "wrong" ) ;
51+ setTimeout ( function ( ) {
52+ hide ( "wrong" ) ;
53+ } , 1000 ) ;
54+ }
55+ }
56+ }
57+ }
58+
59+ //functions
60+
61+ function startCountdown ( ) {
62+ action = setInterval ( function ( ) {
63+ timeremaining -= 1 ;
64+
65+
66+ document . getElementById ( "timeremainingvalue" ) . innerHTML = timeremaining ;
67+ if ( timeremaining == 0 ) {
68+ stopCountdown ( ) ;
69+ show ( "gameOver" ) ;
70+
71+ //game over
72+ document . getElementById ( "gameOver" ) . innerHTML = "<p>Game over!</p><p>Your score is " + score + ".</p>" ;
73+ hide ( "timeremaining" ) ;
74+ hide ( "correct" ) ;
75+ hide ( "wrong" ) ;
76+ playing = false ;
77+
78+ document . getElementById ( "startreset" ) . innerHTML = "Start Game" ;
79+ }
80+ } , 1000 ) ;
81+ }
82+
83+
84+ function stopCountdown ( ) {
85+ clearInterval ( action ) ;
86+ }
87+
88+
89+ function hide ( Id ) {
90+ document . getElementById ( Id ) . style . display = "none" ;
91+ }
92+
93+
94+ function show ( Id ) {
95+ document . getElementById ( Id ) . style . display = "block" ;
96+ }
97+
98+ function generateQA ( ) {
99+ var x = 1 + Math . round ( 9 * Math . random ( ) ) ;
100+ var y = 1 + Math . round ( 9 * Math . random ( ) ) ;
101+ correctAnswer = x * y ;
102+
103+ document . getElementById ( "ques" ) . innerHTML = x + "x" + y ;
104+ var correctPosition = 1 + Math . round ( 3 * Math . random ( ) ) ;
105+
106+ document . getElementById ( "box" + correctPosition ) . innerHTML = correctAnswer ; //correct answer
107+
108+
109+ var answers = [ correctAnswer ] ;
110+
111+ for ( i = 1 ; i < 5 ; i ++ ) {
112+ if ( i != correctPosition ) {
113+ var wrongAnswer ;
114+ do {
115+ wrongAnswer = ( 1 +
116+ Math . round ( 9 * Math . random ( ) ) ) * ( 1 +
117+ Math . round ( 9 * Math . random ( ) ) ) ; //wrong answer
118+
119+ } while ( answers . indexOf ( wrongAnswer ) > - 1 )
120+
121+ document . getElementById ( "box" + i ) . innerHTML = wrongAnswer ;
122+ answers . push ( wrongAnswer ) ;
123+ }
124+ }
125+ }
0 commit comments