-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.html
139 lines (138 loc) · 4.29 KB
/
calculator.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
<html>
<head>
<title>JD's Calculator</title>
<style>
.myCalculator {
table-layout:fixed;
width:40%;
height:80%;
border: 1px solid black;
border-collapse: collapse;
text-align:center;
}
.screen{
height:30%;
text-align:right;
vertical-align: bottom;
padding-right:8px;
background:#000000;
color:#FFFFFF;
font-size:50;
}
.control{
background:#BDBDBD;
cursor:hand;
}
.functions{
background:#FF8000;
cursor:hand;
}
.numbers{
background:#D8D8D8;
cursor:hand;
}
</style>
</head>
<body>
<table class="myCalculator" border= "1">
<tr>
<td class="screen" colspan="4">0</td>
</tr>
<tr class="control">
<td colspan="3">Clear</td><<td class="functions">/</td>
</tr>
<tr class="numbers">
<td>7</td><td>8</td><td>9</td><td class="functions">*</td>
</tr>
<tr class="numbers">
<td>4</td><td>5</td><td>6</td><td class="functions">-</td>
</tr>
<tr class="numbers">
<td>1</td><td>2</td><td>3</td><td class="functions">+</td>
</tr>
<tr class="numbers">
<td>.</td><td>0</td><td>+/-</td><td class="functions">=</td>
</tr>
</table>
<script>
var equation = "";
var isOperation = false;
function initCalc(){
var calc = document.getElementsByClassName("myCalculator")[0];
for(var i=0;i<calc.rows.length;i++){
for(var j=0;j<calc.rows[i].cells.length;j++){
calc.rows[i].cells[j].onclick = function(){
if(!isNaN(parseInt(this.innerHTML))){
numberClick(parseInt(this.innerHTML));
}
else if(this.innerHTML === "Clear"){
clearScreen();
}
else if(this.innerHTML === "/" || this.innerHTML === "*" || this.innerHTML === "-" || this.innerHTML === "+"){
operation(this.innerHTML);
}
else if(this.innerHTML === "+/-"){
negate();
}
else{
equals();
}
};
}
}
}
function numberClick(number){
var screen = document.getElementsByClassName("screen")[0];
if(screen.innerHTML === "0" || isOperation){
screen.innerHTML = number;
}
else{
screen.innerHTML += number.toString();
}
equation += number.toString();
isOperation = false;
}
function clearScreen(){
isOperation = false;
var screen = document.getElementsByClassName("screen")[0];
screen.innerHTML = 0;
equation = "";
}
function operation(operationType){
var screen = document.getElementsByClassName("screen")[0];
//if equation ends in /,x,-,+ then replace it with new operation
if(isOperation){
equation[equation.length-1]=operationType;
}
else{
equation += operationType;
}
isOperation = true;
}
function negate(){
var screen = document.getElementsByClassName("screen")[0];
if(screen.innerHTML !== "0"){
var num = eval(screen.innerHTML + "*-1");
if(num > 0) {
numStr = "+" + num.toString();
}
else {
numStr = num.toString();
}
equation = equation.substring(0,equation.length-Math.abs(parseInt(screen.innerHTML)).toString().length) + numStr;
screen.innerHTML = num;
}
}
function equals(){
var screen = document.getElementsByClassName("screen")[0];
//if equation ends in /,x,-,+ then remove it and perform eval
if(isOperation){
equation[equation.length-1]=operationType;
}
screen.innerHTML = eval(equation);
isOperation = false;
}
document.onload=initCalc();
</script>
</body>
</html>