11// Functions
22
3- function updateOptionLanguage ( id , textCont , value ) {
4- document . getElementById ( id ) . textContent = textCont ;
5- document . getElementById ( "select" ) . querySelector ( `option[value="${ value } "]` ) . textContent = textCont ;
6- }
7-
8- function updateLanguage ( language , translations ) {
3+ function updateLanguage ( ) {
94 const t = translations [ language ] ;
10- document . title = translations [ language ] . title ;
11- document . getElementById ( " inputField" ) . placeholder = t . placeholder ;
12- document . getElementById ( " checkButton" ) . textContent = t . checkButton ;
13- document . getElementById ( " skipButton" ) . textContent = t . skipButton ;
14- document . getElementById ( " skippedExamples" ) . textContent = t . skippedExamples ;
15- document . getElementById ( " solvedExamples" ) . textContent = t . solvedExamples ;
16- document . getElementById ( " mistakes" ) . textContent = t . mistakes ;
17- updateOptionLanguage ( " add10" , t . add10 , "0" ) ;
18- updateOptionLanguage ( " add100" , t . add100 , "1" ) ;
19- updateOptionLanguage ( " add1000" , t . add1000 , "2" ) ;
20- updateOptionLanguage ( " subtraction10" , t . subtraction10 , "3" ) ;
21- updateOptionLanguage ( " subtraction100" , t . subtraction100 , "4" ) ;
22- updateOptionLanguage ( " subtraction1000" , t . subtraction1000 , "5" ) ;
23- updateOptionLanguage ( " multiplication10" , t . multiplication10 , "6" ) ;
24- updateOptionLanguage ( " division10" , t . division10 , "7" ) ;
25- document . getElementById ( " correctly" ) . textContent = t . correctly ;
26- document . getElementById ( " wrongly" ) . textContent = t . wrongly ;
27- document . getElementById ( " correctAnswerP" ) . textContent = t . correctAnswerP ;
5+ document . title = t . title ;
6+ document . getElementById ( ' inputField' ) . placeholder = t . placeholder ;
7+ document . getElementById ( ' checkButton' ) . textContent = t . checkButton ;
8+ document . getElementById ( ' skipButton' ) . textContent = t . skipButton ;
9+ document . getElementById ( ' skippedExamples' ) . textContent = t . skippedExamples ;
10+ document . getElementById ( ' solvedExamples' ) . textContent = t . solvedExamples ;
11+ document . getElementById ( ' mistakes' ) . textContent = t . mistakes ;
12+ document . getElementById ( ' add10' ) . textContent = t . add10 ;
13+ document . getElementById ( ' add100' ) . textContent = t . add100 ;
14+ document . getElementById ( ' add1000' ) . textContent = t . add1000 ;
15+ document . getElementById ( ' subtraction10' ) . textContent = t . subtraction10 ;
16+ document . getElementById ( ' subtraction100' ) . textContent = t . subtraction100 ;
17+ document . getElementById ( ' subtraction1000' ) . textContent = t . subtraction1000 ;
18+ document . getElementById ( ' multiplication10' ) . textContent = t . multiplication10 ;
19+ document . getElementById ( ' division10' ) . textContent = t . division10 ;
20+ document . getElementById ( ' correctly' ) . textContent = t . correctly ;
21+ document . getElementById ( ' wrongly' ) . textContent = t . wrongly ;
22+ document . getElementById ( ' correctAnswerP' ) . textContent = t . correctAnswerP ;
2823}
2924
3025function openSidebar ( ) {
31- document . getElementById ( " sidebar" ) . classList . toggle ( " open" ) ;
32- document . getElementById ( " overlay" ) . classList . toggle ( " open" ) ;
26+ document . getElementById ( ' sidebar' ) . classList . toggle ( ' open' ) ;
27+ document . getElementById ( ' overlay' ) . classList . toggle ( ' open' ) ;
3328}
3429
3530function closeSidebar ( ) {
36- document . getElementById ( " sidebar" ) . classList . remove ( " open" ) ;
37- document . getElementById ( " overlay" ) . classList . remove ( " open" ) ;
31+ document . getElementById ( ' sidebar' ) . classList . remove ( ' open' ) ;
32+ document . getElementById ( ' overlay' ) . classList . remove ( ' open' ) ;
3833}
3934
4035function generateExample ( ) {
4136 a = Math . floor ( ( Math . random ( ) * 90 + 10 ) / variable ) ;
4237 b = Math . floor ( ( Math . random ( ) * 90 + 10 ) / variable ) ;
4338
44- if ( sign === "-" ) {
39+ if ( sign === '-' ) {
4540 if ( a < b ) {
4641 [ a , b ] = [ b , a ] ;
4742 }
48- } else if ( sign === "/" ) {
43+ } else if ( sign === '/' ) {
4944 a = a * b ;
5045 }
5146}
@@ -54,86 +49,86 @@ function resetCounters() {
5449 skipped = 0 ;
5550 solved = 0 ;
5651 mistakes = 0 ;
57- document . getElementById ( " example" ) . textContent = `${ a } ${ sign } ${ b } =` ;
58- document . getElementById ( " inputField" ) . value = "" ;
59- document . getElementById ( " skippedExamples" ) . textContent = `${ skippedText } ${ skipped } ` ;
60- document . getElementById ( " solvedExamples" ) . textContent = `${ solvedText } ${ solved } ` ;
61- document . getElementById ( " mistakes" ) . textContent = `${ mistakesText } ${ mistakes } ` ;
52+ document . getElementById ( ' example' ) . textContent = `${ a } ${ sign } ${ b } =` ;
53+ document . getElementById ( ' inputField' ) . value = '' ;
54+ document . getElementById ( ' skippedExamples' ) . textContent = `${ skippedText } ${ skipped } ` ;
55+ document . getElementById ( ' solvedExamples' ) . textContent = `${ solvedText } ${ solved } ` ;
56+ document . getElementById ( ' mistakes' ) . textContent = `${ mistakesText } ${ mistakes } ` ;
6257}
6358
6459function nextExample ( ) {
6560 generateExample ( ) ;
66- document . getElementById ( " example" ) . textContent = `${ a } ${ sign } ${ b } =` ;
67- document . getElementById ( " inputField" ) . value = "" ;
61+ document . getElementById ( ' example' ) . textContent = `${ a } ${ sign } ${ b } =` ;
62+ document . getElementById ( ' inputField' ) . value = '' ;
6863}
6964
7065function checkExample ( ) {
7166 let correctAnswer ;
7267
73- if ( sign === "+" ) {
68+ if ( sign === '+' ) {
7469 correctAnswer = a + b ;
75- } else if ( sign === "-" ) {
70+ } else if ( sign === '-' ) {
7671 correctAnswer = a - b ;
77- } else if ( sign === "*" ) {
72+ } else if ( sign === '*' ) {
7873 correctAnswer = a * b ;
79- } else if ( sign === "/" ) {
74+ } else if ( sign === '/' ) {
8075 correctAnswer = a / b ;
8176 }
8277
83- if ( Number ( document . getElementById ( " inputField" ) . value ) === correctAnswer ) {
84- document . getElementById ( " correctlySound" ) . play ( ) ;
85- document . getElementById ( " correctlyDiv" ) . style . visibility = " visible" ;
78+ if ( Number ( document . getElementById ( ' inputField' ) . value ) === correctAnswer ) {
79+ document . getElementById ( ' correctlySound' ) . play ( ) ;
80+ document . getElementById ( ' correctlyDiv' ) . style . visibility = ' visible' ;
8681 setTimeout ( ( ) => {
87- document . getElementById ( " correctlyDiv" ) . style . visibility = " hidden" ;
82+ document . getElementById ( ' correctlyDiv' ) . style . visibility = ' hidden' ;
8883 } , 1000 ) ;
8984 solved ++ ;
90- document . getElementById ( " solvedExamples" ) . textContent = `${ solvedText } ${ solved } ` ;
85+ document . getElementById ( ' solvedExamples' ) . textContent = `${ solvedText } ${ solved } ` ;
9186 nextExample ( ) ;
9287 }
93- else if ( document . getElementById ( " inputField" ) . value === "" ) {
88+ else if ( document . getElementById ( ' inputField' ) . value === '' ) {
9489 }
9590 else {
96- document . getElementById ( " wronglySound" ) . play ( ) ;
97- document . getElementById ( " wronglyDiv" ) . style . visibility = " visible" ;
98- document . getElementById ( " correctAnswerP" ) . textContent = `${ correctAnswerPText } ${ correctAnswer } ` ;
91+ document . getElementById ( ' wronglySound' ) . play ( ) ;
92+ document . getElementById ( ' wronglyDiv' ) . style . visibility = ' visible' ;
93+ document . getElementById ( ' correctAnswerP' ) . textContent = `${ correctAnswerPText } ${ correctAnswer } ` ;
9994 setTimeout ( ( ) => {
100- document . getElementById ( " wronglyDiv" ) . style . visibility = " hidden" ;
95+ document . getElementById ( ' wronglyDiv' ) . style . visibility = ' hidden' ;
10196 } , 1000 ) ;
10297 mistakes ++ ;
103- document . getElementById ( " mistakes" ) . textContent = `${ mistakesText } ${ mistakes } ` ;
98+ document . getElementById ( ' mistakes' ) . textContent = `${ mistakesText } ${ mistakes } ` ;
10499 nextExample ( ) ;
105100 }
106101}
107102
108103function skipExample ( ) {
109104 skipped ++ ;
110- document . getElementById ( " skippedExamples" ) . textContent = `${ skippedText } ${ skipped } ` ;
105+ document . getElementById ( ' skippedExamples' ) . textContent = `${ skippedText } ${ skipped } ` ;
111106 nextExample ( ) ;
112107}
113108
114- function changeMode ( newSign , newVariable , value , optionName ) {
109+ function changeMode ( newSign , newVariable , optionName ) {
115110 sign = newSign ;
116111 variable = newVariable ;
117112
118- document . getElementById ( "select" ) . value = value ;
113+ document . getElementById ( 'modeName' ) . textContent = translations [ language ] [ optionName ] ;
119114
120115 generateExample ( ) ;
121116 resetCounters ( ) ;
122117 closeSidebar ( ) ;
123118
124- document . querySelectorAll ( " .sidebarOption" ) . forEach ( option => {
119+ document . querySelectorAll ( ' .sidebarOption' ) . forEach ( option => {
125120 option . disabled = false ;
126- option . classList . remove ( " open" ) ;
121+ option . classList . remove ( ' open' ) ;
127122 } ) ;
128123
129124 document . getElementById ( optionName ) . disabled = true ;
130- document . getElementById ( optionName ) . classList . toggle ( " open" ) ;
125+ document . getElementById ( optionName ) . classList . toggle ( ' open' ) ;
131126}
132127
133- document . addEventListener ( " keydown" , function ( event ) {
134- if ( event . key === " Enter" ) {
135- checkExample ( ) ;
136- }
128+ document . addEventListener ( ' keydown' , function ( event ) {
129+ if ( event . key === ' Enter' ) {
130+ checkExample ( ) ;
131+ }
137132} )
138133
139134// App logic
@@ -146,20 +141,28 @@ let a, b, sign, variable;
146141let skippedText , solvedText , mistakesText , correctAnswerPText ;
147142
148143const browserLanguage = navigator . language . slice ( 0 , 2 ) ;
144+ let translations ;
145+ let language ;
149146
150- fetch ( " translations.json" )
147+ fetch ( ' translations.json' )
151148 . then ( response => response . json ( ) )
152- . then ( translations => {
153- if ( translations [ browserLanguage ] ) {
154- updateLanguage ( browserLanguage , translations ) ;
149+ . then ( data => {
150+ translations = data ;
151+ if ( translations [ browserLanguage ] && browserLanguage != 'en' ) {
152+ console . log ( browserLanguage ) ;
153+ language = browserLanguage ;
154+ updateLanguage ( ) ;
155+ } else {
156+ language = 'en' ;
157+ console . log ( 'GGG' ) ;
155158 }
156159
157- skippedText = document . getElementById ( " skippedExamples" ) . textContent ;
158- solvedText = document . getElementById ( " solvedExamples" ) . textContent ;
159- mistakesText = document . getElementById ( " mistakes" ) . textContent ;
160- correctAnswerPText = document . getElementById ( " correctAnswerP" ) . textContent ;
160+ skippedText = document . getElementById ( ' skippedExamples' ) . textContent ;
161+ solvedText = document . getElementById ( ' solvedExamples' ) . textContent ;
162+ mistakesText = document . getElementById ( ' mistakes' ) . textContent ;
163+ correctAnswerPText = document . getElementById ( ' correctAnswerP' ) . textContent ;
161164
162- changeMode ( "+" , 1 , "1" , " add100" ) ;
165+ changeMode ( '+' , 1 , ' add100' ) ;
163166 } )
164167 . catch ( error => {
165168 console . error ( error ) ;
0 commit comments