-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal.html
210 lines (199 loc) · 5.58 KB
/
cal.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!-- Write a program to build the Calculator in JavaScript. -->
<!DOCTYPE html>
<html>
<head>
<title>Calculator Program in JavaScript</title>
<!-- Begins the JavaScript Code -->
<script>
// Use insert() function to insert the number in textview.
function insert(num) {
documentdocument.form1.textview.value =
document.form1.textview.value + num;
}
// Use equal() function to return the result based on passed values.
function equal() {
var exp = document.form1.textview.value;
if (exp) {
document.form1.textview.value = eval(exp);
}
}
/* Here, we create a backspace() function to remove the number at the end of the numeric series in textview. */
function backspace() {
var exp = document.form1.textview.value;
document.form1.textview.value = exp.substring(
0,
exp.length - 1
); /* remove the element from total length ? 1 */
}
</script>
<!-- Start the coding for CSS -->
<style>
/* Create the Outer layout of the Calculator. */
.formstyle {
width: 300px;
height: 330px;
margin: 20px auto;
border: 3px solid skyblue;
border-radius: 5px;
padding: 20px;
text-align: center;
background-color: grey;
}
/* Display top horizontal bar that contain some information. */
h1 {
text-align: center;
padding: 23px;
background-color: skyblue;
color: white;
}
input:hover {
background-color: green;
}
* {
margin: 0;
padding: 0;
}
/* It is used to create the layout for calculator button. */
.btn {
width: 50px;
height: 50px;
font-size: 25px;
margin: 2px;
cursor: pointer;
background-color: red;
color: white;
}
/* It is used to display the numbers, operations and results. */
.textview {
width: 223px;
margin: 5px;
font-size: 25px;
padding: 5px;
background-color: lightgreen;
}
</style>
</head>
<body>
<h1>Calculator Program in JavaScript</h1>
<div class="formstyle">
<form name="form1">
<input class="textview" name="textview" />
</form>
<center>
<table>
<tr>
<td>
<input
class="btn"
type="button"
value="C"
onclick="form1.textview.value = ' ' "
/>
</td>
<td>
<input
class="btn"
type="button"
value="B"
onclick="backspace()"
/>
</td>
<td>
<input
class="btn"
type="button"
value="/"
onclick="insert('/')"
/>
</td>
<td>
<input
class="btn"
type="button"
value="x"
onclick="insert('*')"
/>
</td>
</tr>
<tr>
<td>
<input class="btn" type="button" value="7" onclick="insert(7)" />
</td>
<td>
<input class="btn" type="button" value="8" onclick="insert(8)" />
</td>
<td>
<input class="btn" type="button" value="9" onclick="insert(9)" />
</td>
<td>
<input
class="btn"
type="button"
value="-"
onclick="insert('-')"
/>
</td>
</tr>
<tr>
<td>
<input class="btn" type="button" value="4" onclick="insert(4)" />
</td>
<td>
<input class="btn" type="button" value="5" onclick="insert(5)" />
</td>
<td>
<input class="btn" type="button" value="6" onclick="insert(6)" />
</td>
<td>
<input
class="btn"
type="button"
value="+"
onclick="insert('+')"
/>
</td>
</tr>
<tr>
<td>
<input class="btn" type="button" value="1" onclick="insert(1)" />
</td>
<td>
<input class="btn" type="button" value="2" onclick="insert(2)" />
</td>
<td>
<input class="btn" type="button" value="3" onclick="insert(3)" />
</td>
<td rowspan="5">
<input
class="btn"
style="height: 110px"
type="button"
value="="
onclick="equal()"
/>
</td>
</tr>
<tr>
<td colspan="2">
<input
class="btn"
style="width: 106px"
type="button"
value="0"
onclick="insert(0)"
/>
</td>
<td>
<input
class="btn"
type="button"
value="."
onclick="insert('.')"
/>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>