-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (88 loc) · 2.4 KB
/
script.js
File metadata and controls
111 lines (88 loc) · 2.4 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
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
$(document).ready(function() {
var inputs = [''];
var subInput = [''];
var currNum;
var finalDisplay;
var operators = ['+', '-', '*', '/'];
var operator = {
"add": '+',
"subtract": '-',
"multiply": '*',
"divide": '/',
"dot": '.'
};
var operator1 = {
"add": '+',
"subtract": '-',
"multiply": '*',
"divide": '/',
};
function update() {
finalDisplay = inputs.join('');
subInput = finalDisplay.split(/[^0-9.]/);
currNum = subInput[subInput.length - 1];
if(currNum.length>9 || inputs.length > 21){
$("#display").html("0");
$("#subDisp").html("Digits limit reached");
inputs = [''];
return;
}
if (Object.values(operator1).indexOf(inputs[inputs.length - 1]) !== -1){
$("#display").html(inputs[inputs.length - 1]);
$("#subDisp").html(finalDisplay);
}else{
$("#display").html(currNum);
$("#subDisp").html(finalDisplay);
}
}
function getOperator(input) {
if (inputs.length === 1 && input !== operator.dot) {
console.log("Not allowed");
return;
} else if (inputs[inputs.length - 1] !== operator.dot && input === operator.dot) {
finalDisplay = inputs.join('');
subInput = finalDisplay.split(/[^0-9.]/);
currNum = subInput[subInput.length - 1];
if (currNum.includes(".")) {
console.log("Not allowed");
} else {
inputs.push(input);
}
} else {
if (Object.values(operator).indexOf(inputs[inputs.length - 1]) !== -1) {
console.log("Not Allowed");
//return;
} else {
inputs.push(input);
}
}
update();
}
function getTotal() {
finalDisplay = inputs.join('');
$("#display").html(eval(finalDisplay));
}
//keypad event handler
$("#0,#1,#2,#3,#4,#5,#6,#7,#8,#9").on("click", function() {
inputs.push(this.id);
update();
});
//Eraser fucntion
$("#clearAll,#clearOne").on('click', function() {
if (this.id === 'clearOne') {
inputs.pop();
update();
} else {
inputs = [''];
update();
}
});
//Operators fucntion
$("#add,#subtract,#multiply,#divide,#dot,#equals").on('click', function() {
if (this.id === 'equals') {
getTotal();
} else {
getOperator(operator[this.id]);
}
});
});