-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFull code
103 lines (99 loc) · 2.34 KB
/
Full code
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Javascript Calculator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class = "container">
<h1>Calculator</h1>
<div class="calculator">
<input type="text" name="screen" id="search">
<table>
<tr>
<td><button>(</button></td>
<td><button>)</button></td>
<td><button>C</button></td>
<td><button>%</button></td>
</tr>
<tr>
<td><button>7</button></td>
<td><button>8</button></td>
<td><button>9</button></td>
<td><button>X</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>
<style>
.container {
text-align: center;
margin: top:23px;
}
table {
margin: auto;
}
input {
font-size: 34px;
border: 5px solid black;
border-radius: 4px;
}
button {
font-size: 30px;
background: aliceblue;
width: 102px;
height: 58px;
}
.calculator {
display: inline-block;
}
</style>
</body>
<script src = "index.js"></script>
<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=='X'){
buttonText = '*';
screenValue += buttonText;
screen.value += buttonText;
}
else if (buttonText == 'C'){
screen.value = "";
screen.value = screenValue;
}
else if (buttonText == '='){
screen.value = eval(screenValue);
}
else{
screenValue = buttonText;
screen.value = screenValue;
}
})
}
</script>
</html>