-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathCalculator.html
117 lines (109 loc) · 3.1 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
<!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>
</head>
<style>
.container{
text-align: center;
margin-top:23px
}
table{
margin: auto;
}
input{
border-radius: 21px;
border: 4px solid #000000;
font-size:34px;
height: 50px;
width: 300px;
}
button{
border-radius: 20px;
font-size: 40px;
background: #978fa0;
width: 60px;
height: 55px;
margin: 6px;
}
.calculator{
border: 4px solid #000000;
background-color: #5d4c5c;
padding: 23px;
border-radius: 53px;
display: inline-block;
}
h1{
font-size: xxx-large;
font-family: 'Courier New', Courier, monospace;
}
</style>
<body>
<div class="container">
<h1>Calculator</h1>
<div class="calculator">
<input type="text" name="screen" id="screen">
<table>
<tr>
<td><button>(</button></td>
<td><button>)</button></td>
<td><button>C</button></td>
<td><button>x</button></td>
</tr>
<tr>
<td><button>7</button></td>
<td><button>8</button></td>
<td><button>9</button></td>
<td><button>*</button></td>
</tr>
<tr>
<td><button>4</button></td>
<td><button>5</button></td>
<td><button>6</button></td>
<td><button>-</button></td>
</tr>
<tr>
<td><button>1</button></td>
<td><button>2</button></td>
<td><button>3</button></td>
<td><button>+</button></td>
</tr>
<tr>
<td><button>0</button></td>
<td><button>.</button></td>
<td><button>/</button></td>
<td><button>=</button></td>
</tr>
</table>
</div>
</div>
<script>
let screen = document.getElementById('screen');
buttons = document.querySelectorAll('button');
let screenValue = '';
for (item of buttons) {
item.addEventListener('click', (e) => {
buttonText = e.target.innerText;
console.log('Button text is ', buttonText);
if (buttonText == 'C') {
screenValue = "";
screen.value = screenValue;
}
else if(buttonText == 'x') {
screen.value = screen.value.substr(0, screen.value.length - 1);
}
else if (buttonText == '=') {
screen.value = eval(screenValue);
}
else {
screenValue += buttonText;
screen.value = screenValue;
}
})
}
</script>
</body>
</html>