-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
168 lines (155 loc) · 4.34 KB
/
Copy pathscript.js
File metadata and controls
168 lines (155 loc) · 4.34 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use strict";
function add(num1, num2) {
return num1 + num2;
};
function subtract(num1, num2) {
return num1 - num2;
};
function multiply(num1, num2) {
return num1 * num2;
};
function divide(num1, num2) {
return num1 / num2;
};
function operate(num1, num2, operation) {
let result;
switch (operation) {
case "add" :
result = add(num1, num2);
break;
case "subtract" :
result = subtract(num1, num2);
break;
case "multiply" :
result = multiply(num1, num2);
break;
case "divide" :
if (num2 === 0) {
return "Maths Error";
};
result = divide(num1, num2);
break;
default :
result = num2; //returns current display if no operator clicked, x==x
};
return result;
};
function produceOutput() {
num2 = displayValue;
let result;
if (num2 !== "") {
result = operate(Number(num1), Number(num2), operation);
if (typeof(result)!="string") {//if Error thrown don't try to round it
result = Math.round(result*"1e8")*"1e-8";
if (result > Number.MAX_SAFE_INTEGER) {
result = result.toExponential(8);
};
//round to 8dp -- represent as exponential if too large to be accurate
};
} else if (num2==="") {
/*this will occur if they change their mind on the operator
i.e. they click a new operator without entering num2, triggering
an implicit calculation.
We do not need to calculate anything in this case, so as follows:*/
result = num1; //result, which becomes new num1, is set to old num1
};
display.textContent = result;
displayValue = display.textContent;
calcSoFar.textContent = "";
operation = "";
isOpSelected = false;
};
function reset() {
num1 = "0";
num2 = "0";
operation = "";
isOpSelected = false;
display.textContent = "0";
displayValue = display.textContent;
calcSoFar.textContent = "";
};
function switchToNum2() { //called when operation selected
isOpSelected = true;
num1 = displayValue;
display.textContent = "";
displayValue = display.textContent;
if (num1 == "Maths Error") { //stops errors being moved to upper display
num1 = "0";
};
};
function performButton(target) {
const btnClass = target.className;
if (display.textContent == "Maths Error") {
display.textContent="0";
displayValue = display.textContent;
//clear box if an error screen when user types next calc in
};
if (btnClass.slice(0,3)=="num"){
if (displayValue==="0") {
display.textContent = btnClass.slice(3,4);
displayValue = display.textContent;
} else {
display.textContent = displayValue + btnClass.slice(3,4);
displayValue = display.textContent;
};
}
else if (btnClass=="point") {
if (!displayValue.includes(".")) { //if number is not already a decimal
display.textContent = displayValue + ".";
displayValue = display.textContent;
};
}
else if (btnClass.slice(0,8)=="operator") {
if (isOpSelected===true) {
//2 numbers chosen already - firstly implicitly calculate result
//then below select the operator and take previous result as num1
produceOutput();
};
if (btnClass.slice(9)=="add") {
operation = "add";
switchToNum2();
calcSoFar.textContent = num1+" +";
}
else if (btnClass.slice(9)=="subtract") {
operation = "subtract";
switchToNum2();
calcSoFar.textContent = num1+" -";
}
else if (btnClass.slice(9)=="multiply") {
operation = "multiply";
switchToNum2();
calcSoFar.textContent = num1+" ×";
}
else if (btnClass.slice(9)=="divide") {
operation = "divide";
switchToNum2();
calcSoFar.textContent = num1+" ÷";
};
}
else if (btnClass=="equals") {
produceOutput();
isOpSelected = false;
}
else if (btnClass=="del") {
display.textContent = displayValue.slice(0,-1);
displayValue = display.textContent;
}
else if (btnClass=="clear") {
reset();
};
};
function addListeners() {
const btnList = document.querySelectorAll("button");
btnList.forEach(node => {
node.addEventListener("click", evnt => performButton(evnt.target))
});
};
const display = document.querySelector(".display>.current-num>span");
let displayValue = display.textContent;
const calcSoFar = document.querySelector(".display>.calc-so-far>span");
let num1;
let num2;
let operation;
let isOpSelected = false;
reset();
addListeners();