-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
141 lines (122 loc) · 4.83 KB
/
main.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
$(document).ready(() => {
let expression = [];
let currentNumber = "";
let mathExp = [];
let keyMappings = {
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
"add": "+",
"subtract": "-",
"multiply": "*",
"divide": "/",
"equals": "=",
"decimal": "."
};
$("#clear").click(() => {
$("#display").html("0");
$("#expression").html("");
expression = [];
mathExp = [];
currentNumber = "";
})
try {
for (let key of Object.keys(keyMappings)) {
let digitRegex = new RegExp(/^\d$/g);
let operatorRegex = new RegExp(/^[+/*-]$/g);
if (digitRegex.test(keyMappings[key])) {
$("#" + key).click(() => {
currentNumber += keyMappings[key];
currentNumber = parseFloat(currentNumber).toString()
expression.push(keyMappings[key]);
if (digitRegex.test(expression[expression.length - 1]))
$("#display").html(expression.join(""));
else
$("#display").html(parseFloat(currentNumber).toString());
$("#expression").html(expression.join(""));
});
} else {
if (key == "equals") {
$("#equals").click(() => {
let number = [];
for (let i = expression.length - 1; i >= 0; i--) {
if (operatorRegex.test(expression[i]))
break;
else
number.unshift(expression[i]);
}
mathExp.push(parseFloat(number.join("")).toString());
let result = parseFloat(eval(mathExp.join(""))).toString();
$("#display").html(result);
$("#expression").html(expression.join("") + "=" + result);
mathExp.splice(mathExp.length - 1, 1);
currentNumber = result;
expression = [result];
mathExp = [];
});
} else if (key == "decimal") {
$("#decimal").click(() => {
if (currentNumber.indexOf(".") == -1 && currentNumber.length > 0) {
currentNumber += keyMappings[key];
expression.push(keyMappings[key]);
}
$("#display").html(currentNumber);
$("#expression").html(expression.join(""));
});
} else {
$("#" + key).click(() => {
if (operatorRegex.test(expression[expression.length - 1])) {
if (key == "subtract")
mathExp.push(keyMappings[key]);
else
mathExp[mathExp.length - 1] = keyMappings[key];
expression.push(keyMappings[key]);
} else if (currentNumber.length > 0 && !operatorRegex.test(expression[expression.length - 1])) {
mathExp.push(parseFloat(currentNumber).toString());
currentNumber = "";
mathExp.push(keyMappings[key]);
expression.push(keyMappings[key]);
}
$("#display").html(keyMappings[key]);
$("#expression").html(expression.join(""));
});
}
}
}
$(document).keypress(function(event) {
let keyCode = (event.keyCode ? event.keyCode : event.which);
let keyCodeMappings = {
"13": "equals",
"42": "multiply",
"43": "add",
"45": "subtract",
"46": "decimal",
"47": "divide",
"48": "zero",
"49": "one",
"50": "two",
"51": "three",
"52": "four",
"53": "five",
"54": "six",
"55": "seven",
"56": "eight",
"57": "nine",
"61": "equals",
"88": "multiply",
"120": "multiply"
};
if (keyCodeMappings.hasOwnProperty(keyCode))
$("#" + keyCodeMappings[keyCode]).click();
})
} catch(e) {
window.alert('Enter a valid expression.');
}
});