-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscript.js
138 lines (115 loc) · 4.71 KB
/
script.js
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
let result = document.querySelector(".result");
let clear = document.querySelector(".clear");
let equal = document.querySelector(".equal");
let allBtn = document.querySelectorAll(".btn");
const theme_selecor = document.querySelector("#theme-selector");
/**
* Validates and formats user input in an input field.
*
* @param {Event} event - The user input event.
*/
function validateInput(event) {
let inputValue = event.target.value;
const operators = /[+\-*/^%]/;
inputValue = inputValue.replace(/,/g, ".");
inputValue = inputValue.replace(/[^0-9+\-*/.^%e]/g, "");
// Validate that scientific notation is correct (e.g. 1e+10)
if (/[e][+\-]?\d+$/.test(inputValue)) {
const parts = inputValue.split(/[e]/);
const mantissa = parts[0];
const exponent = parts[1];
if (/[+\-]/.test(exponent)) {
// The exponent can have a "+" or "-" sign
if (/[^0-9]/.test(exponent.slice(1))) {
// If the exponent has characters other than numbers after the sign
inputValue = mantissa + "e" + "0";
}
} else if (/[^\d]/.test(exponent)) {
// If the exponent has characters that are not numbers
inputValue = mantissa + "e" + "0";
}
}
// Separate consecutive "e" characters with "*"
inputValue = inputValue.replace(/e+/g, (match) => match.split("").join("*"));
// Add a "0" before the first non-minus operator if the first character is an operator
if (operators.test(inputValue[0]) && inputValue[0] !== "-") {
inputValue = "0" + inputValue;
}
// Replace the last sign with a new one if another sign is added
const lastChar = inputValue[inputValue.length - 2];
if (operators.test(lastChar) && operators.test(event.data)) {
inputValue = inputValue.slice(0, -2) + event.data;
}
//code to handle decimal value calculation
inputValue = inputValue.replace(/(\d+\.\d*)\./g, "$1");
result.value = inputValue;
}
result.addEventListener("input", validateInput);
allBtn.forEach((input) => {
input.addEventListener("click", () => {
if (
(input.textContent === `0` || input.textContent === `.`) &&
result.value.length === 0
) {
return;
}
result.value += input.textContent;
validateInput({ target: { value: result.value }, data: input.textContent });
});
});
equal.addEventListener("click", () => {
if (result.value !== "") {
if (result.value.includes("^")) {
result.value = result.value.replace("^", "**");
}
result.value = eval(result.value);
} else {
alert("Enter valid input");
}
});
clear.addEventListener("click", () => {
result.value = "";
});
result.addEventListener("keyup", (event) => {
if (event.key === "Enter" && result.value !== "") {
result.value = eval(result.value);
}
});
// Add an event listener for the backspace button.
const backspace = document.querySelector(".backspace");
backspace.addEventListener("click", () => {
if (result.value.length > 0) {
result.value = result.value.slice(0, -1); // Remove the last character
}
});
// Add an event listener for the theme selector.
theme_selecor.addEventListener("change", (event) => {
// Get all the number buttons, and set their "theme" attribute to the selected theme.
const number_buttons = document.querySelectorAll(".num");
number_buttons.forEach((button) => {
button.setAttribute("theme", event.target.value);
});
// Get all the operator buttons, and set their "theme" attribute to the selected theme.
const operator_buttons = document.querySelectorAll(".operator");
operator_buttons.forEach((button) => {
button.setAttribute("theme", event.target.value);
});
// Get the calc container, and set its "theme" attribute to the selected theme.
const calc_container = document.querySelector(".calc");
calc_container.setAttribute("theme", event.target.value);
// Get the result input, and set its "theme" attribute to the selected theme.
const result_input = document.querySelector(".result");
result_input.setAttribute("theme", event.target.value);
// Get the clear button, and set its "theme" attribute to the selected theme.
const clear_button = document.querySelector(".clear");
clear_button.setAttribute("theme", event.target.value);
// Get the equal button, and set its "theme" attribute to the selected theme.
const equal_button = document.querySelector(".equal");
equal_button.setAttribute("theme", event.target.value);
// Get the element with ID "msg", and set its "theme" attribute to the selected theme.
const msg = document.querySelector("#msg");
msg.setAttribute("theme", event.target.value);
// Get the element with class "header", and set its "theme" attribute to the selected theme.
const header = document.querySelector(".header");
header.setAttribute("theme", event.target.value);
});