-
Notifications
You must be signed in to change notification settings - Fork 0
/
newjs.js
103 lines (71 loc) · 2.99 KB
/
newjs.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
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
/*
buttons color change when clicked
select each button(queryselector),
loop through the nodelist and add eventlisteners mousedown and mouseup event listeners to add and remove the class name with color
*/
let btnclick= document.querySelectorAll('td');
for (var i=0 ; i<btnclick.length; i++) {
btnclick[i].addEventListener('mousedown', function() {
this.classList.add('tdhover');
});
btnclick[i].addEventListener('mouseup',function() {
this.classList.remove('tdhover')
})
}
let screen= document.getElementById('display');
//CALCULATOR COMPUTE
let arr=[]
let noclicktwice=/[-+*/]/
let screened=document.querySelector ('#display');
function updatescreen() {
let screencontent= document.querySelector ('#display');
display.textContent=arr.join(" ");
}
let dialkeys= document.querySelectorAll('td');
for (let i = 0; i < dialkeys.length; i++) {
dialkeys[i].addEventListener('click',function (event) {
if (this.className==="num-btn") {
if (arr.length===0 || noclicktwice.test(arr[arr.length-1])==true) {
arr.push(this.textContent);
updatescreen();
}
else{
arr[arr.length-1]+= this.textContent.toString();
updatescreen()
}
}
if (this.className==="operator" && arr.length !==0 && noclicktwice.test(arr[arr.length-1]) !==true ) {
arr.push(this.textContent);
updatescreen();
}
if (this.className==="AC") {
arr=[];
updatescreen();
}
else if (this.className==="CE") {
if (noclicktwice.test(arr[arr.length-1])==true){
arr.pop();
updatescreen();
}
else{
split=arr[arr.length-1].split("");
split.pop();
arr.pop();
arr.push(split.join(""));
updatescreen()
}
}
if (this.id==="equal") {
screened.textContent=eval(arr.join(" "));
arr=[];
}
if (this.id==="point") {
if ( arr.length !==0 && noclicktwice.test(arr[arr.length-1]) !==true ) {
arr[arr.length-1.]+= this.textContent;
updatescreen()
}
}
}
)
}
document.onload