-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
159 lines (142 loc) · 5.02 KB
/
Copy pathscript.js
File metadata and controls
159 lines (142 loc) · 5.02 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
// Function to center the calculator
function centerCalculator() {
let elem = document.querySelector('.container');
elem.style.left = "50%";
elem.style.top = "50%";
elem.style.transform = "translateX(-50%) translateY(-50%)";
}
// Function to uncenter the clock(i.e., originsl position)
function uncenterCalculator() {
let elem = document.querySelector('.container');
elem.style.left = "5px";
elem.style.top = "0%";
elem.style.removeProperty('transform');
}
// Function to change position of the clock in points
function choosePos() {
let elem = document.querySelector('.container');
let top_pos = parseInt(prompt("Enter the top position", "5"));
let left_pos = parseInt(prompt("Enter the left position", "5"));
if (isNaN(top_pos) || isNaN(left_pos)) {
alert("Please enter valid numbers!\nCannot change position.");
return;
}
move(elem, top_pos, left_pos);
}
function move(elem, top_pos, left_pos){
elem.style.position = "absolute";
elem.style.removeProperty('transform');
if (left_pos > document.documentElement.clientWidth-elem.clientWidth) {
elem.style.left = (document.documentElement.clientWidth-elem.clientWidth) + 'px';
}
else {
elem.style.left = left_pos + 'px';
}
if (top_pos > document.documentElement.clientHeight-elem.clientHeight) {
elem.style.top = (document.documentElement.clientHeight-elem.clientHeight) + 'px';
}
else {
elem.style.top = top_pos + 'px';
}
if (left_pos < 0) {
elem.style.left = '0px';
}
if (top_pos < 0) {
elem.style.top = '0px';
}
}
let target_mousedown;
let target_mouseup;
document.documentElement.addEventListener("mousedown", (event) => {target_mousedown = event.target});
document.documentElement.addEventListener("mouseup", move_with_mouse);
function move_with_mouse(event) {
target_mouseup = event.target;
let elem = document.querySelector('.container');
// Do not move if clicked inside the container or on ignored buttons
if (target_mouseup === target_mousedown){
if (!elem.contains(event.target) && !event.target.hasAttribute("data-ignore-move")) {
move(elem, event.clientY, event.clientX);
}
}
}
function appendNum(button) {
let display = document.querySelector(".display");
let value = button.dataset.realValue;
if (value === "sqrt") {
display.value += "√(";
} else if (["sin", "cos", "tan"].includes(value)) {
display.value += `${value}(`;
} else if (value === "exp") {
display.value += "exp(";
} else if (value === "log") {
display.value += "log10(";
} else if (value === "ln") {
display.value += "ln(";
} else if (value === "^") {
display.value += "^";
} else {
display.value += value;
}
}
// Attach event listener to all buttons : Event Delegation
document.querySelectorAll(".button").forEach(button => {
if (button.hasAttribute("data-real-value")) {
button.addEventListener("click", () => {
appendNum(button);
});
}
});
function calculate() {
let display = document.querySelector(".display");
if (!display.value) {
display.value = "Empty Field";
setTimeout(clearDisplay, 2000);
return;
}
let expression = display.value;
// First, ensure balanced parentheses
let openBrackets = (expression.match(/\(/g) || []).length;
let closeBrackets = (expression.match(/\)/g) || []).length;
while (openBrackets > closeBrackets) {
expression += ")";
closeBrackets++;
}
// Fix: Handle trigonometric functions first (degrees to radians)
expression = expression.replace(/(sin|cos|tan)\(([^)]+)\)/g, (match, func, angle) => {
return `Math.${func}(${angle} * Math.PI / 180)`;
});
// Convert functions to JavaScript Math functions
expression = expression
.replace(/√\(/g, "Math.sqrt(")
.replace(/exp\(/g, "Math.exp(")
.replace(/log10\(/g, "Math.log10(")
.replace(/ln\(/g, "Math.log(")
.replace(/(\d+)\^(\d+)/g, "Math.pow($1, $2)");
try {
let result = eval(expression);
display.value = (Math.round(result * 1000) / 1000).toString();
} catch (error) {
console.error("Calculation error:", error);
console.log("Expression being evaluated:", expression);
display.value = "Invalid Expression";
setTimeout(clearDisplay, 2000);
}
}
function clearDisplay() {
document.getElementById("box-16").value = "";
}
let target;
function mousedown(event) {
target = event.target;
target.style.backgroundColor = "rgb(174, 237, 255)";
target.style.boxShadow = "none";
}
function mouseup() {
target.style.removeProperty('background-color');
target.style.boxShadow = "2px 2px 2px";
}
window.onload = function () {
let buttons = document.querySelectorAll(".button");
buttons.forEach(button => button.addEventListener("mousedown", mousedown));
document.documentElement.addEventListener("mouseup", mouseup);
};