Skip to content

Commit 8b04a52

Browse files
committed
Calculator
1 parent c56cf31 commit 8b04a52

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

Calculator/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Calculator</title>
7+
<link rel="stylesheet" href="style.css" >
8+
</head>
9+
<body>
10+
11+
<div id="calculator">
12+
<input id="display" readonly>
13+
<div id="keys">
14+
<button onclick="appendToDisplay('+')" class="operator-btn">+</button>
15+
<button onclick="appendToDisplay('7')">7</button>
16+
<button onclick="appendToDisplay('8')">8</button>
17+
<button onclick="appendToDisplay('9')">9</button>
18+
<button onclick="appendToDisplay('-')" class="operator-btn">-</button>
19+
<button onclick="appendToDisplay('4')">4</button>
20+
<button onclick="appendToDisplay('5')">5</button>
21+
<button onclick="appendToDisplay('6')">6</button>
22+
<button onclick="appendToDisplay('*')" class="operator-btn">*</button>
23+
<button onclick="appendToDisplay('1')">1</button>
24+
<button onclick="appendToDisplay('2')">2</button>
25+
<button onclick="appendToDisplay('3')">3</button>
26+
<button onclick="appendToDisplay('/')" class="operator-btn">/</button>
27+
<button onclick="appendToDisplay('0')">0</button>
28+
<button onclick="appendToDisplay('.')">.</button>
29+
<button onclick="calculate()">=</button>
30+
<button onclick="clearDisplay()" class="operator-btn">C</button>
31+
32+
33+
</div>
34+
35+
36+
</div>
37+
38+
39+
40+
<script src="index.js"></script>
41+
</body>
42+
</html>

Calculator/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// CALCULATOR PROGRAM
2+
3+
const display = document.getElementById("display");
4+
5+
6+
7+
8+
function appendToDisplay(input){
9+
display.value += input;
10+
}
11+
12+
function clearDisplay(){
13+
display.value = "";
14+
}
15+
16+
function calculate(){
17+
try{
18+
display.value = eval(display.value);
19+
}
20+
catch(error){
21+
display.value = "Error";
22+
}
23+
}

Calculator/style.css

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
body {
3+
4+
margin: 0;
5+
display: flex;
6+
justify-content: center;
7+
align-items: center;
8+
height: 100vh;
9+
background-color: hsl(0, 0%, 95%);
10+
}
11+
12+
13+
#calculator {
14+
font-family: Arial, sans-serif;
15+
background-color: hsl(0, 0%, 15%);
16+
border-radius: 15px;
17+
max-width: 500px;
18+
overflow: hidden;
19+
}
20+
21+
22+
#display {
23+
width: 100%;
24+
padding: 20px;
25+
font-size: 5rem;
26+
text-align: left;
27+
border: none;
28+
background-color: black;
29+
color :white
30+
}
31+
32+
33+
#keys {
34+
display: grid;
35+
grid-template-columns: repeat(4, 1fr);
36+
gap: 10px;
37+
padding: 25px;
38+
}
39+
40+
button {
41+
width: 100px;
42+
height: 100px;
43+
border-radius: 50px;
44+
border: none;
45+
background-color: hsl(0, 0%, 30%);
46+
color: white;
47+
font-size: 3rem;
48+
font-weight: bold;
49+
cursor: pointer;
50+
}
51+
52+
button:hover{
53+
background-color: red;
54+
}
55+
56+
button:active{
57+
background-color: rebeccapurple;
58+
}
59+
60+
.operator-btn{
61+
background-color: orange;
62+
}

0 commit comments

Comments
 (0)