-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
81 lines (75 loc) · 2.73 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
</head>
<body>
<form id="form_calc" action="#">
<select name="type" id="oil_price">
<option value="45">95</option>
<option value="42">92</option>
<option value="50">ДТ</option>
</select>
<input id="oil_liters" type="number" name="count" placeholder="объем">
<input id="oil_summ" type="number" name="sum" placeholder="сумма">
</form>
<script>
let state = {
price: 45, // Тип бензина, само название не важно, важна именно цена
liters: 0,
summ: 0,
};
const $oilLiters = $('#oil_liters');
const $oilPrice = $('#oil_price');
const $oilSumm = $('#oil_summ');
function render(actualState) {
$oilLiters.val(Math.ceil(actualState.liters));
$oilPrice.val(actualState.price);
$oilSumm.val(Math.ceil(actualState.summ));
}
function calculate(actualState, event) {
switch (event.changedProp) {
case 'liters':
return {
price: actualState.price,
liters: event.value,
summ: actualState.price * event.value
};
case 'price':
return {
price: event.value,
liters: actualState.liters,
summ: event.value * actualState.liters
};
case 'summ':
return {
price: actualState.price,
liters: event.value / actualState.price,
summ: event.value
};
default:
return actualState;
}
}
function createListener(propName) {
return function(event) {
state = calculate(state, {
changedProp: propName,
value: parseInt($(event.target).val()) || 0,
});
render(state);
}
}
$oilLiters.on('input', createListener('liters'));
$oilPrice.on('change', createListener('price'));
$oilSumm.on('input', createListener('summ'));
</script>
</body>
</html>