-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
182 lines (176 loc) · 5.96 KB
/
Copy pathindex.html
File metadata and controls
182 lines (176 loc) · 5.96 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
@font-face {
font-family: 'Propisi';
src: url('../kidmath/Propisi.TTF') format('truetype');
}
body {
margin: 0;
padding: 0;
background: linear-gradient( #bbb, transparent 1px), linear-gradient( 90deg, #bbb, transparent 1px);
background-size: 40px 40px;
background-position: top left;
font-family: 'Propisi', Fallback, sans-serif;
}
h1 {
font-size: 80pt;
line-height: 90px;
text-align: center;
margin: 0;
margin-bottom: 80px;
height: 80px;
}
a {
font-size: 44pt;
font-weight: bold;
line-height: 40px;
text-decoration: none;
margin-right: 40px;
}
a:hover {
color: orangered;
}
#task {
display: flex;
margin: 0px;
margin-bottom: 40px;
padding: 0px;
/* outline: 1px solid red; */
}
#result {
font-size: 44pt;
font-weight: bold;
line-height: 40px;
margin-left: 40px;
}
.cell_sign {
width: 40px;
height: 40px;
text-align: right;
font-size: 32pt;
/* font-weight: bold; */
line-height: 40px;
font-family: Arial, Helvetica, sans-serif;
/* outline: 1px solid lightgray; */
}
.cell1 {
width: 40px;
height: 40px;
text-align: right;
font-size: 44pt;
font-weight: bold;
line-height: 40px;
letter-spacing: 4px;
/* outline: 1px solid lightgray; */
}
.selected {
color: green;
}
</style>
</head>
<body>
<h1>Математика 2 класс</h1>
<div id="task">
<div id="digit11" class="cell1">1</div>
<div id="digit12" class="cell1">2</div>
<div id="operation" class="cell_sign">+</div>
<div id="digit21" class="cell1">2</div>
<div id="digit22" class="cell1">3</div>
<div id="equal" class="cell_sign">=</div>
<div id="answer1" class="cell1"></div>
<div id="answer2" class="cell1"></div>
<div id="result"></div>
</div>
<div>
<a href="#" id="button_check">Проверить</a>
<a href="#" id="button_clear">Стереть</a>
<a href="#" id="button_update">Обновить</a>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
var operations = [
{
'sign': '+',
'func': function(a, b) { return a + b; },
'val1': function() { return Math.floor(Math.random() * 90); },
'val2': function(val1) { return Math.floor(Math.random() * (100 - val1)); }
},
{
'sign': '-',
'func': function(a, b) { return a - b; },
'val1': function() { return Math.floor(10 + Math.random() * 90); },
'val2': function(val1) { return Math.floor(Math.random() * val1); }
}
];
var rightAnswer = -1;
var answer = -1;
function updateAnswer(val){
answer = val;
if (answer >= 0){
var digit1 = Math.floor(answer / 10);
$('#answer1').text(digit1 > 0 ? digit1 : '');
$('#answer2').text(Math.floor(answer % 10));
} else {
$('#answer1').text('');
$('#answer2').text('');
}
}
function checkAnswer(){
if (answer == rightAnswer){
$('#result').css('color', 'green').text('Верно!');
} else {
$('#result').css('color', 'orangered').text('Неверно!');
}
}
function generateTask(){
var operationIndex = Math.floor(Math.random() * operations.length);
var operation = operations[operationIndex];
var value1 = operation.val1();
var value2 = operation.val2(value1);
rightAnswer = operation.func(value1, value2);
var digit11 = Math.floor(value1 / 10);
$('#digit11').text(digit11 > 0 ? digit11 : '');
$('#digit12').text(Math.floor(value1 % 10));
var digit21 = Math.floor(value2 / 10);
$('#digit21').text(digit21 > 0 ? digit21 : '');
$('#digit22').text(Math.floor(value2 % 10));
$('#operation').text(operation.sign);
}
$('#button_update').click(function(e){
e.preventDefault();
generateTask();
$('#result').text('');
});
$('#button_clear').click(function(e){
e.preventDefault();
updateAnswer(-1);
$('#result').text('');
});
$('#button_check').click(function(e){
e.preventDefault();
checkAnswer();
});
document.addEventListener('keypress', (event) => {
console.log(event.keyCode);
if (event.keyCode == 13 && answer >= 0){
checkAnswer();
}
if (event.keyCode == 32){
updateAnswer(-1);
$('#result').text('');
}
if (event.key >= '0' && event.key <= '9') {
var newAnswer = answer >= 0 ? answer * 10 + parseInt(event.key) : parseInt(event.key);
if (newAnswer < 100) updateAnswer(newAnswer);
}
});
$(function()
{
generateTask();
});
</script>
</body>
</html>