Create a Calculator - [Vanilla JS]

Problem

Build a web calculator, that supports the basic operations of add, remove, multiply and divide.

In addition, support resetting the current state, as well as a decimal point button.

Implementation

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

<html>
<head>
<style>
.wrapper{
width: 400px;
height: 400px;
margin: 15px auto;
border: 1px solid black;
}
.display-panel{
margin: 10px auto;
width: 95%;
height: 60px;
line-height: 60px;
font-size: 30px;
border: 1px solid black;
text-align: right;
overflow-y: hidden;
overflow-x: scroll;
white-space: nowrap;
}

.ctl-panel{
margin: 5px auto;
width: 95%;
height: 300px;
border: 1px solid black;
font-size: 30px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}

.left-part{
border: 1px solid red;
flex: 1 1 70%;
display: grid;
justify-content: space-around;
align-content: space-around;
grid-template-columns: auto auto auto;
grid-template-rows: auto auto auto auto;
}


.right-part{
flex: 1 1 20%;
border: 1px solid red;
display: grid;
justify-content: space-around;
align-content: space-around;
grid-template-columns: auto;
grid-template-rows: 30% 60%;
}

#equal{
height: 100%;
}

button{
width: 50px;
height: 50px;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="display-panel" id="display">
display placeholdersdsfsdfsdfsdffsdfsdffsdf
</div>
<div class="ctl-panel" id="panel">
<div class='left-part'>
<div><button>+</button></div>
<div><button>-</button></div>
<div><button>&times;</button></div>
<div><button>7</button></div>
<div><button>8</button></div>
<div><button>9</button></div>
<div><button>4</button></div>
<div><button>5</button></div>
<div><button>6</button></div>
<div><button>1</button></div>
<div><button>2</button></div>
<div><button>3</button></div>
<div><button>0</button></div>
<div><button>.</button></div>
<div><button>C</button></div>
</div>
<div class='right-part'>
<div><button>&divide;</button></div>
<div><button id="equal">&equals;</button></div>
</div>
</div>
</div>

<script>
function handleInput(e){
let val = e.target.textContent
let display = document.getElementById("display")
if(isNum(val)){
handleNum(val, display);
}else if(isDot(val)){
handleDot(val, display);
}else if(isOperator(val)){
handleOperator(val, display)
}else if(isEqual(val)){
handleCalc(display)
}else if(isClear(val)){
handleClear(display)
}else{
return
}
}

function handleClear(display){
display.textContent = ""
}

function handleNum(v, display){
let oldText = display.textContent
display.textContent += v
}

function handleOperator(v, display){
let oldText = display.textContent
if( isOperator(oldText[oldText.length-1]) )
oldText=oldText.substr(0,oldText.length-1)
display.textContent = oldText+v
}

function handleDot(v, display){
let oldText = display.textContent
if( isDot(oldText[oldText.length-1]) ) return
display.textContent += v
}


function handleCalc(display){
let oldText = display.textContent
if( !isNum(oldText[oldText.length-1]) ) {
display.textContent = 0
return
}
display.textContent = calc(oldText)
}

function calc( text ){
let stack = []
let numbers = text.split(/\+|\-|\×|\÷/)
let operators = text.replace(/[0-9]|\./g, "").split("")
console.log(numbers)
console.log(operators)

stack.push(new Number(numbers[0]) )
for( let i = 0; i < operators.length; i++ ){
let num = new Number( numbers[i+1] )
if(operators[i] === "+"){
stack.push(num)
}else if(operators[i] === "-"){
stack.push(-num)
}else if(operators[i] === "×"){
let top = stack.pop()
stack.push(top*num)
}else if(operators[i] === "÷"){
let top = stack.pop()
stack.push(top/num)
}
}
console.log(stack)
return stack.reduce((item,accum)=>{
return accum + item
},0)
}


function isNum(v){ return v >= "0" && v <= "9" }
function isClear(v){ return v === 'C' }
function isEqual(v){ return v === '=' }
function isDot(v){ return v === '.' }
function isOperator(v){
return v === '+' || v === '-' || v === '÷' || v === '×'
}

document.getElementById("panel").addEventListener( "click", handleInput )
</script>
</body>
</html>