-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (51 loc) · 1.42 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const input = document.querySelector('input');
const digitBtns = document.querySelectorAll('.btns')
const clr = document.querySelector('.clear')
const eql = document.querySelector('.equal')
const back = document.querySelector('.bk')
const ans = document.querySelector('span')
const op = document.querySelectorAll('.op')
let bool = false;
let total = 0;
function calAns(){
if(input.value === "") ans.innerText = "= "+total;
else ans.innerText = "= " + eval(`${ans.innerText}${input.value}`)
input.value = "";
}
input.onkeyup = e => {
(e.key === "Enter")? calAns():undefined
// console.log(e)
}
// document.onkeydown = e => console.log(e)
digitBtns.forEach( btn => {
btn.onclick = function (e){
input.value += this.getAttribute('data-value');
}
})
clr.onclick = () => {
input.value = ""
ans.innerText = "0"
bool = false;
};
eql.onclick = () => {
(input.value !== "" || ans.innerText!=='0')? calAns(): undefined
bool = false
}
back.onclick = () => {
if(input.value !== "") input.value = input.value.substr(0, input.value.length - 1)
}
op.forEach(btn => {
btn.onclick = function(){
let attr = this.getAttribute('data-value');
if(!bool){
input.value += attr
bool = true;
}else{
if(ans.innerText !== '0'){
ans.innerText = eval(`${ans.innerText}${input.value}`) + (attr)? attr : '';
}
else ans.innerText = eval(input.value) + attr
input.value = ''
}
}
})