-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
41 lines (40 loc) · 1.32 KB
/
script.js
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
const display = document.querySelector('.display')
const controlButton = document.querySelector('.controls').children
const allSymbols =['+','-','x','/','=','.','c']
let firstValue = ''
let secondValue= ''
let symbol = ''
let result = ''
function calculate(){
firstValue = parseFloat(firstValue)
secondValue = parseFloat(secondValue)
if(symbol==='+') result = firstValue+secondValue
if (symbol==='-') result = firstValue-secondValue
if (symbol==='x') result = firstValue * secondValue
if (symbol==='/') result = firstValue/secondValue
display.innerText= result
firstValue=result
secondValue=""}
for(let button of controlButton) {
button.addEventListener('click',()=>{
const {innerText: btnValue} = button
const btnValueIsSymbol = allSymbols.includes(btnValue)
if(btnValueIsSymbol && firstValue) {
secondValue && calculate()
symbol = btnValue
}
else if(!symbol)firstValue +=btnValue
else if(symbol)secondValue +=btnValue
if(btnValue === 'c'){
firstValue=""
secondValue=""
symbol=""
result= ""
display.innerText=""
} else{
if(btnValue!== '=') {
display.innerText += btnValue
}
}
})
}