-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpew.js
103 lines (90 loc) · 2.29 KB
/
pew.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
const num = document.querySelectorAll('.num');
const screen = document.querySelector('.screen');
const ops = document.querySelectorAll('.ops');
const equal = document.querySelector('#equal');
const del = document.querySelector('#delete');
const reset = document.querySelector('#reset');
const numArr = [];
const inputArr = [];
for(let i = 0; i < num.length; i++) {
num[i].addEventListener('click', showNum);
}
for(let i = 0; i < ops.length; i++) {
ops[i].addEventListener('click', operateNum);
}
del.addEventListener('click', (e) => {
numArr.pop();
screen.innerHTML = numArr.join('');
});
reset.addEventListener('click', wipeAll);
equal.addEventListener('click', giveAnswer);
function showNum(e) {
numArr.push(this.innerText);
screen.innerHTML = numArr.join('');
checkDecimal();
}
function operateNum(e) {
if(screen.innerHTML != '') {
inputArr.push(+`${screen.innerHTML}`, this.innerText);
while(numArr.length > 0) {
numArr.pop();
}
if(inputArr[2] != null) {
inputResult(solveEquation());
inputArr.push(+`${screen.innerHTML}`,this.innerText);
}
}
else {
screen.innerHTML = 0;
}
}
function wipeAll(e) {
screen.innerHTML = '';
fullWipe();
}
function giveAnswer(e) {
if(screen.innerHTML != '') {
inputArr.push(+`${screen.innerHTML}`);
inputResult(solveEquation());
}
else {
screen.innerHTML = 0;
}
}
function solveEquation() {
const sign = inputArr[1];
const firstNum = inputArr[0];
const secondNum = inputArr[2];
if(sign == 'x') {
return firstNum * secondNum;
}
if(sign == '/') {
if(secondNum == 0) {
return 'Invalid';
}
return firstNum / secondNum;
}
if(sign == '+') {
return firstNum + secondNum;
}
if(sign == '-') {
return firstNum - secondNum;
}
}
function inputResult(num) {
fullWipe();
const roundedNum = Math.round(num * 100) / 100;
screen.innerHTML = roundedNum;
}
function fullWipe() {
while(numArr.length > 0 || inputArr.length > 0) {
numArr.pop();
inputArr.pop();
}
}
function checkDecimal() {
if(numArr.includes('.'))
num[10].disabled = true;
else
num[10].disabled = false;
}