-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (46 loc) · 1.66 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import '/style.css'
const equal=document.getElementById("equal")
const result=document.querySelector("#result")
const clear=document.getElementById("clear")
const deleteNum=document.getElementById("deleteNum")
const operand=document.querySelector("#operand") // click num,operation show operand div i.e 1+2
const numbers=document.querySelectorAll("[data-operand]")
const splNumber=document.querySelector("[data-splOperand]")
const operation=document.querySelectorAll("[data-operation]")
const appendText=(numbers)=>{ //every number click to show #operand div
operand.textContent+= numbers
}
const addNumberList=(numEls)=>{ //every number click appendText fn
numEls.addEventListener("click",()=>{
appendText(parseInt(numEls.innerText))
})
}
numbers.forEach((num)=>{ //numbers buttons one by one pass arg addNumberList fn.
addNumberList(num)
})
splNumber.addEventListener("click",(e)=>{ // dot num
appendText(e.target.innerText)
})
operation.forEach((opeEl)=>{ //operations buttons click pass arg appendText fn.
opeEl.addEventListener("click",()=>{
appendText(opeEl.innerText)
})
})
equal.addEventListener("click",()=>{ //equal button click event
try {
result.innerText= eval(operand.innerText)
} catch (error) {
result.innerText="expression error!"
}
})
clear.addEventListener("click",()=>{ //clear btn click event
operand.innerText=''
result.innerText=''
})
deleteNum.addEventListener("click",()=>{ //delete each operand value btn click event
let operTxt=operand.innerText
let operArr=[... operTxt]
operArr.pop()
let deleteOpr= operArr.join("")
operand.innerText=deleteOpr
})