-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
74 lines (53 loc) · 1.79 KB
/
node.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
// ____ _ _ _____ _
// | _ \| | | | / ____| | |
// | |_) | | __ _ ___| | _| (___ _ _ __| | ___
// | _ <| |/ _` |/ __| |/ /\___ \| | | |/ _` |/ _ \
// | |_) | | (_| | (__| < ____) | |_| | (_| | (_) |
// |____/|_|\__,_|\___|_|\_|_____/ \__,_|\__,_|\___/ Sudan Dev -->
// # by Omer A. Bila
// # FB : https://web.facebook.com/BlackSudo :D
var keys = document.querySelectorAll('#calculator span');
var operators = ['+', '-', '*', '/'];
var decimalAdded = false;
for(var i = 0; i < keys.length; i++) {
keys[i].onclick = function(e) {
var input = document.querySelector('.screen');
var inputVal = input.innerHTML;
var btnVal = this.innerHTML;
if(btnVal == 'AC') {
input.innerHTML = '';
decimalAdded = false;
}
else if(btnVal == '=') {
var equation = inputVal;
var lastChar = equation[equation.length - 1];
equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
if(operators.indexOf(lastChar) > -1 || lastChar == '.')
equation = equation.replace(/.$/, '');
if(equation)
input.innerHTML = eval(equation);
decimalAdded = false;
}
else if(operators.indexOf(btnVal) > -1) {
var lastChar = inputVal[inputVal.length - 1];
if(inputVal != '' && operators.indexOf(lastChar) == -1)
input.innerHTML += btnVal;
else if(inputVal == '' && btnVal == '-')
input.innerHTML += btnVal;
if(operators.indexOf(lastChar) > -1 && inputVal.length > 1) {
input.innerHTML = inputVal.replace(/.$/, btnVal);
}
decimalAdded =false;
}
else if(btnVal == '.') {
if(!decimalAdded) {
input.innerHTML += btnVal;
decimalAdded = true;
}
}
else {
input.innerHTML += btnVal;
}
e.preventDefault();
}
}