-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.html
More file actions
89 lines (83 loc) · 2.57 KB
/
Copy pathcalc.html
File metadata and controls
89 lines (83 loc) · 2.57 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculator</title>
<style>
#number1, #number2, #result2{
width: 200px;
height: 50px;
font-size: 25px;
display: inline-block;
vertical-align: middle;
}
#but, #operation, #help{
width: 100px;
height: 50px;
display: inline-block;
vertical-align: middle;
margin: 0 10px;
}
</style>
</head>
<body>
<input id="number1">
<select name="" id="operation" required>
<option value="0">+</option>
<option value="1">-</option>
<option value="2">*</option>
<option value="3">/</option>
<option value="4">%</option>
</select>
<input id="number2">
<input id="but" type="button" onclick="javascript:calculate();" value="=">
<input id="help" type="button" onclick="javascript:help();" value="help">
<div id="result2"> </div>
<script>
var opArray = ['+','-','*','/','%'];
function help(){
var help = '';
var counter = 0;
while (counter < opArray.length){
help+= opArray[counter] + ' моя функция \n\n';
counter++;
}
alert(help)
}
function calculate(){
var el1 = document.getElementById("number1"),
el2 = document.getElementById("number2"),
op = document.getElementById("operation");
if (el1 && el2 && op){
var left = parseInt(el1.value),
right = parseInt(el2.value),
operator = op.value;
if (!isNaN(left) && !isNaN(right)){
switch (opArray[+operator]){
case "+": setResult(left + right);
break;
case "-": setResult(left - right);
break;
case "*": setResult(left * right);
break;
case "/": setResult(left / right);
break;
case "%": setResult(left % right);
break;
default: alert("Wrong operation");
}
} else{
alert("Error");
}
} else {
alert("no element");
}
}
function setResult(res) {
document.getElementById("result2").innerHTML = "<b>Result is</b>: " + res;
}
</script>
</body>
</html>