-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
162 lines (127 loc) · 3.83 KB
/
Copy pathscript.js
File metadata and controls
162 lines (127 loc) · 3.83 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
function add(...numbers) {
return numbers.reduce((element, total) => (total += element), 0);
}
function subtract(...numbers) {
return numbers.reduce((previous, current) => previous - current);
}
function multiply(...numbers) {
return numbers.reduce((previous, current) => previous * current);
}
function divide(...numbers) {
return numbers.reduce((previous, current) => previous / current);
}
const helper = document.querySelector("#helper");
const display = document.querySelector("#display");
const numberButtons = document.querySelectorAll(".key");
numberButtons.forEach((button) =>
button.addEventListener("click", () => updateDisplay(button.textContent))
);
const operators = document.querySelectorAll(".operator");
operators.forEach((button) =>
button.addEventListener("click", () => evaluate(button.textContent))
);
const clearButton = document.querySelector("#clear");
clearButton.addEventListener("click", reset);
const delButton = document.querySelector("#del");
delButton.addEventListener("click", del);
function del() {
display.textContent = display.textContent.slice(0, -1);
}
display.textContent = "";
// Calculator evaluates only a pair of numbers at a time, i.e. "12 + 7 - 5 * 3 = should yield 42."
// 12 + 7 = 19
// 19 - 5 = 14
// 14 * 3 = 42
let pair = [];
let operator;
function reset() {
display.textContent = "";
helper.textContent = "";
pair = [];
operator = undefined;
}
function updateDisplay(clickedNumber) {
if (clickedNumber == ".") {
if (display.textContent.includes(".")) return;
if (display.textContent == "") {
display.textContent += "0.";
} else {
display.textContent += ".";
}
return;
}
if (display.textContent == "0") {
if (clickedNumber == "0") {
return;
} else {
display.textContent = "";
}
}
display.textContent += clickedNumber;
}
function evaluate(inputOperator) {
// Handle inputting a negative first number
if (operator == undefined && pair[0] == undefined) {
if (inputOperator == "-" && display.textContent == "") {
display.textContent += "-";
} else if (inputOperator == "+" && display.textContent == "-") {
display.textContent = "";
}
}
if (operator == undefined && pair[0] != undefined) {
// can't '=' a single number
if (inputOperator != "=") {
operator = inputOperator;
helper.textContent += ` ${operator} `;
}
}
// Add a number to the pair
if (
pair.length < 2 &&
display.textContent != "" &&
display.textContent != "0" &&
!isNaN(display.textContent)
) {
if (inputOperator == "=" && operator == undefined) {
return;
}
pair.push(display.textContent);
helper.textContent += ` ${display.textContent} `;
}
// Operate on the pair
if (pair.length == 2 && display.textContent != "") {
pair = pair.map((x) => +x);
let [a, b] = [...pair];
if (a == 0 && b == 0 && operator == ":") {
alert("Not on my watch!");
reset();
return;
}
let evaluatePair = {
"+": add(a, b),
"-": subtract(a, b),
x: multiply(a, b),
":": divide(a, b),
};
// The result becomes the first number in pair in next operation
pair = [roundResult(evaluatePair[operator])];
helper.textContent += ` = `;
helper.textContent += ` ${pair[0]} `;
display.textContent = "";
b = undefined;
operator = undefined;
}
let operators = ["+", "-", "x", ":"];
// User decides to change the operator, i.e. 3 + -> 3 -
if (operators.includes(inputOperator) && pair[0] != undefined) {
if (operators.includes(helper.textContent.trimEnd().slice(-1))) {
helper.textContent = helper.textContent.slice(0, -2);
}
operator = inputOperator;
helper.textContent += ` ${operator} `;
display.textContent = "";
}
}
function roundResult(result) {
return Math.round(result * 1000) / 1000;
}