forked from facn5/ObaydaHananCl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (61 loc) · 1.99 KB
/
Copy pathapp.js
File metadata and controls
65 lines (61 loc) · 1.99 KB
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
var buttons = document.body.querySelectorAll('.buttons > button');
var output = document.querySelector('.window');
var operator = ['×', '÷', '-', '+', '%'];
var input = '';
var operatorFlag = false;
var dotFlag = false;
var equation = '';
var result = '';
var acBtn = buttons[0];
var i;
for (i = 0; i < buttons.length; i++) {
buttons[i].onclick = function () {
var btnText = this.innerHTML;
if (btnText === '^'){
btnText = '**';
}
if (btnText === 'AC') {
input = '';
operatorFlag = false;
equation = '';
acBtn.innerHTML = "CE";
} else if (btnText === 'CE') {
input = input.slice(0, input.length - 1);
} else if (btnText === '.') {
if (input.indexOf('.') === -1 || dotFlag) {
input += '.';
dotFlag = false;
}
} else if (btnText === '=') {
if (operator.indexOf(input[input.length - 1]) > -1) {
input = input.slice(0, input.length - 1);
}
equation = input.replace(/×/g, '*');
equation = equation.replace(/÷/g, '/');
result = Math.round(eval(equation)*1000000)/1000000;
input = result;
operatorFlag = true;
acBtn.innerHTML = "AC";
} else if (operator.indexOf(btnText) > -1) {
if (operatorFlag) {
input += btnText;
operatorFlag = false;
} else {
input = input.slice(0, input.length - 1) + btnText;
}
dotFlag = true;
}else {
if (result !== '' && operator.indexOf(input[input.length - 1]) > -1) {
input += btnText;
result = '';
} else if (result !== '') {
input = btnText;
result = '';
} else {
input += btnText;
}
operatorFlag = true;
}
output.innerHTML = input;
};
}