-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (127 loc) · 3.96 KB
/
script.js
File metadata and controls
149 lines (127 loc) · 3.96 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
/**
* @license
* Copyright © 2026 Ashraf Morningstar
* https://github.com/AshrafMorningstar
*
* Licensed under the MIT License.
* This is a personal educational recreation.
* Original project concepts remain property of their respective creators.
*/
/*
Copyright (c) 2026 Ashraf Morningstar
These are personal recreations of existing projects, developed by Ashraf Morningstar
for learning and skill development.
Original project concepts remain the intellectual property of their respective creators.
Repository: https://github.com/AshrafMorningstar
*/
// Screen element reference
const screen = document.getElementById('displayResult');
screen.value = '0';
// Limit for input length
const INPUT_LIMIT = 20;
// Counter for open parentheses
let openParentheses = 0;
function handleInput(input) {
if (screen.value === 'Error') {
resetScreen();
}
// Handle special cases for math functions
if (typeof input === 'string' && input.startsWith('Math.')) {
if (screen.value === '0') {
screen.value = input;
} else {
screen.value += input;
}
if (input.endsWith('(')) {
openParentheses++;
}
return;
}
// Prevent multiple decimal points in a number
if (input === '.' && screen.value.split(/[\+\-\*\/]/).pop().includes('.')) {
return;
}
// Enforce input length limit
if (screen.value.length >= INPUT_LIMIT) {
return;
}
if (screen.value === '0' && input !== '.') {
screen.value = input;
} else {
screen.value += input;
}
}
function resetScreen() {
screen.value = '0';
openParentheses = 0;
}
function removeLastChar() {
if (screen.value === 'Error') {
resetScreen();
return;
}
if (screen.value.length > 1) {
// Adjust parentheses counter when removing parentheses
if (screen.value.endsWith('(')) {
openParentheses--;
} else if (screen.value.endsWith(')')) {
openParentheses++;
}
screen.value = screen.value.slice(0, -1);
} else {
screen.value = '0';
}
}
function evaluateExpression() {
try {
// Auto-close any unclosed parentheses
let expr = screen.value;
while (openParentheses > 0) {
expr += ')';
openParentheses--;
}
// Replace symbols for multiplication and division
expr = expr.replace(/×/g, '*').replace(/÷/g, '/');
// Validate the expression
if (!/^[0-9+\-*/(). Math\w]+$/.test(expr)) {
throw new Error('Invalid input');
}
// Evaluate and format the result
let result = eval(expr);
// Handle division by zero
if (!isFinite(result)) {
throw new Error('Cannot divide by zero');
}
// Format the result
if (Number.isInteger(result)) {
screen.value = result.toString();
} else {
screen.value = parseFloat(result.toFixed(8)).toString();
}
} catch (err) {
screen.value = 'Error';
setTimeout(resetScreen, 2000);
}
}
// Add keyboard functionality
document.addEventListener('keydown', (e) => {
const key = e.key;
// Prevent default actions for calculator keys
if (key.match(/[\d+\-*/.=Enter]/) || key === 'Backspace' || key === 'Escape') {
e.preventDefault();
}
// Handle input keys
if (key.match(/[\d.]/)) {
handleInput(key);
} else if (key.match(/[+\-*\/]/)) {
handleInput(key);
} else if (key === 'Enter' || key === '=') {
evaluateExpression();
} else if (key === 'Backspace') {
removeLastChar();
} else if (key === 'Escape') {
resetScreen();
}
});
// These are personal recreations of existing projects, developed by Ashraf Morningstar for learning and skill development. Original project concepts remain the intellectual property of their respective creators.
https://github.com/AshrafMorningstar