-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlab1.html
94 lines (94 loc) · 2.48 KB
/
lab1.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
82
83
84
85
86
87
88
89
90
91
92
93
94
<!doctype html>
<html>
<head>
<style>
body{
font-family:sans-serif;
}
.container{
width:330px;
height:395px;
border:1px solid blue;
margin-left:37%;
}
.result{
height:50px;
border:1px solid blue;
margin:2%;
font-size:38px;
}
.numbers{
width:315px;
height:280px;
border:1px solid blue;
margin:2%;
}
button{
width:60px;
height:50px;
margin:2%;
color:blue;
}
</style>
</head>
<body>
<h1 style="text-align:center;">Simple Calculator</h1>
<div class="container">
<div class="result">
<label id="total" style="margin-top:-5%;"></label>
</div><br><br>
<div class="numbers">
<button onclick="calculate('1')">1</button>
<button onclick="calculate('2')">2</button>
<button onclick="calculate('3')">3</button>
<button onclick="calculate('+')">+</button><br>
<button onclick="calculate('4')">4</button>
<button onclick="calculate('5')">5</button>
<button onclick="calculate('6')">6</button>
<button onclick="calculate('-')">-</button><br>
<button onclick="calculate('7')">7</button>
<button onclick="calculate('8')">8</button>
<button onclick="calculate('9')">9</button>
<button onclick="calculate('*')">*</button><br>
<button onclick="equal('=')">=</button>
<button onclick="calculate('0')">0</button>
<button onclick="calculate('c')">CE</button>
<button onclick="calculate('/')">/</button>
</div>
<div id="error"
style="display:none;color:red;margin-top:-10%;margin-left:20%;"></div>
</div>
<footer>
<h5 style="margin-left:42%;">Made with <span style="color:red;">♥</span>
by Sujith D</h5>
</footer>
<script>
var expression = "";
function calculate(value){
if(value === 'c'){
document.getElementById('total').innerHTML = "";
expression = "";
document.getElementById('error').style.display = 'none';
}else{
expression+=value;
document.getElementById('total').innerHTML = expression;
}
}
function equal(){
try{
var result = eval(expression);
if(isNaN(result) || !isFinite(result)){
document.getElementById('error').innerHTML = "cannot divide by zero";
document.getElementById('error').style.display = 'block';
}else{
document.getElementById('total').innerHTML = result;
expression = result;
}
}catch(err){
document.getElementById('error').innerHTML = "Enter a valid expression";
document.getElementById('error').style.display = 'block';
}
}
</script>
</body>
</html>