Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
115 lines (99 loc) · 2.53 KB
/
Copy pathscript.js
File metadata and controls
115 lines (99 loc) · 2.53 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
112
113
114
$(document).ready(onClick);
function onClick() {
var count = null; //Number of digits before a decimal point
var indicator = false; // To test if screen is empty
var prevNum = null;
var currNum = 0;
var tempNum = null;// Intermediate number
var tempOp = null; // Intermediate operator
var nextOp = null;
var currOp = null; // Operation between prevNum and currNum
$("#decimal").on("click", function(event) {
if(count === null) {
count = 0;
}
$("#cal").append(".");
});
$(".num").on( "click", function(event) {
if(indicator) {
count = null; numInt = 0; prevNum = null; currNum = 0; tempNum = null; tempOp = null; nextOp = null; currOp = null;
$("#screen").html(0);
$("#cal").html(' ');
indicator = false;
}
if(count === null) {
currNum = currNum * 10 + parseInt(event.target.innerHTML);
}
if(count !== null) {
count++;
currNum = currNum + parseInt(event.target.innerHTML) / Math.pow(10,count);
}
$("#cal").append(event.target.innerHTML);
});
$(".sym").on("click", function(event) {
nextOp = event.target.innerHTML;
$("#cal").append(' ' + nextOp + ' ');
if(count !== null) {
count = null;
}
if(currOp === null) {
prevNum = currNum;
currNum = 0;
currOp = nextOp;
return;
}
if(tempOp !== null) {
currNum = calculate(tempOp, tempNum, currNum);
tempNum = null;
tempOp = null;
}
if(isAdditive(nextOp) || !isAdditive(currOp) || isEqual(nextOp)) {
prevNum = calculate(currOp, prevNum, currNum);
currOp = nextOp;
currNum = 0;
$("#screen").html(prevNum);
if(prevNum.toString().length >= 10) {
$('#screen').css({
fontSize: '28px'
});
}
return;
}
// Multiplicative operation following an additive operation:
tempNum = currNum; tempOp = nextOp; currNum = 0; nextOp = null;
});
$("#equal").on("click", function(event) {
indicator = true;
if(prevNum.toString().length >= 10) {
$('#screen').css({
fontSize: '24px'
});
}
});
$("#clear").on("click", function() {
count = null; numInt = 0; prevNum = null; currNum = 0; tempNum = null; tempOp = null; nextOp = null; currOp = null;
$("#screen").html(0);
$("#cal").html(' ');
indicator = false;
});
}
function isEqual(operator) {
return operator === "=";
}
function isAdditive(operator) {
return operator === "+" || operator === "-";
}
function calculate(operator, x, y) {
if(operator === "x") {
return x * y;
}
if (operator === "÷") {
return x / y;
}
if (operator === "+") {
return x + y;
}
if (operator === "-") {
return x - y;
}
}