-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
147 lines (130 loc) · 3.59 KB
/
engine.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
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
const display1El = document.querySelector('.d1');
const display2El = document.querySelector('.d2');
const display3El = document.querySelector('.d3');
const numbersEl = document.querySelectorAll('.number');
const operationsEl = document.querySelectorAll('.operation');
const equalEl = document.querySelector('.equals');
const clearAllEl = document.querySelector('.clear-all');
const clearLastEl = document.querySelector('.clear-last');
let dis1Num = '';
let dis2Num = '';
let resultNum = '';
let lastOperation = null;
let haveDot = false;
numbersEl.forEach (number => {
number.addEventListener('click', (e)=>{
if (e.target.innerText === '.' && !haveDot){
haveDot = true;
}else if(e.target.innerText === '.' && haveDot){
return;
}
dis2Num += e.target.innerText;
display2El.innerText = dis2Num;
})
});
operationsEl.forEach(operation =>{
operation.addEventListener ('click', (e)=>{
if (!dis2Num) return;
haveDot=false;
const operationName = e.target.innerText;
if(dis1Num && dis2Num && lastOperation){
mathOperation();
}else{
result= parseFloat(dis2Num);
}
clearVar(operationName);
lastOperation = operationName;
console.log(result);
})
});
function clearVar(name = " ") {
dis1Num += dis2Num +" "+ name +" ";
display1El.innerText = dis1Num;
display2El.innerText = "";
dis2Num="";
display3El.innerText = result;
}
function mathOperation(){
if (lastOperation === '/') {
result = parseFloat(result) / parseFloat(dis2Num);
}else if (lastOperation === 'X') {
result = parseFloat(result) * parseFloat(dis2Num);
}else if (lastOperation === '-') {
result = parseFloat(result) - parseFloat(dis2Num);
}else if (lastOperation === '+') {
result = parseFloat(result) + parseFloat(dis2Num);
}else if (lastOperation === '%') {
result = (parseFloat(result) / parseFloat(dis2Num))* 100;
}
}
equalEl.addEventListener("click", () => {
if (!dis2Num || !dis1Num) return;
haveDot = false;
mathOperation();
clearVar();
display2El.innerText = result;
display3El.innerText = "";
dis2Num = result;
dis1Num = "";
});
clearAllEl.addEventListener('click', (e) => {
display1El.innerText = '0';
display2El.innerText = '0';
display3El.innerText = '0';
dis1Num = '';
dis2Num = '';
result = '';
})
clearLastEl.addEventListener('click', (e) => {
display2El.innerText = '';
dis2Num = '';
})
window.addEventListener('keydown', (e)=> {
if(
e.key === '0' ||
e.key === '1' ||
e.key === '2' ||
e.key === '3' ||
e.key === '4' ||
e.key === '5' ||
e.key === '6' ||
e.key === '7' ||
e.key === '8' ||
e.key === '9' ||
e.key === '.'
){
clickButtonEl(e.key)
} else if(
e.key === '+' ||
e.key === '-' ||
e.key === '/' ||
e.key === '%'
){
clickOperation(e.key);
}
else if (e.key === '*'){
clickOperation('X');
}
else if (e.key === 'Enter'|| e.key === '='){
clickEqual();
}
});
function clickButtonEl(key){
numbersEl.forEach(button =>{
if(button.innerText === key){
button.click();
}
})
}
function clickOperation(key){
operationsEl.forEach(button =>{
if(button.innerText === key){
button.click();
}
})
}
function clickEqual(){
equalEl.click();
}
if(numbersEl.click()){
}