-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
302 lines (266 loc) · 8.07 KB
/
Script.js
File metadata and controls
302 lines (266 loc) · 8.07 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// ===================================================
// CalcAI — script.js
// AI Calculator Logic
// ===================================================
// ===== STATE =====
let current = '0'; // Jo number screen pe dikh raha hai
let prev = ''; // Pehla number (operator se pehle)
let operator = ''; // +, -, *, /
let justCalced = false; // Abhi = daba ke aaya?
// ===== DOM ELEMENTS =====
const resultEl = document.getElementById('result');
const exprEl = document.getElementById('expression');
const histEl = document.getElementById('history');
const aiMsgEl = document.getElementById('aiMsg');
// ===== OPERATOR SYMBOL MAP =====
const opSymbol = { '*': '×', '/': '÷', '-': '−', '+': '+' };
// ===================================================
// DISPLAY UPDATE
// ===================================================
function updateDisplay() {
// Main result
resultEl.textContent = current;
// Auto font size based on length
const len = current.replace('-', '').replace('.', '').length;
resultEl.className = 'result-line ' + (
len <= 7 ? 'size-xl' :
len <= 10 ? 'size-lg' :
len <= 14 ? 'size-md' : 'size-sm'
);
// Expression line (e.g. "5 ×")
if (prev && operator) {
exprEl.textContent = prev + ' ' + opSymbol[operator];
} else {
exprEl.textContent = '';
}
}
// Pop animation on result
function flashPop() {
resultEl.classList.remove('pop');
void resultEl.offsetWidth; // reflow trick
resultEl.classList.add('pop');
}
// ===================================================
// AI TIPS
// ===================================================
const tips = [
"Wah! Bahut badhiya! 🎯",
"Bilkul sahi calculation! ✅",
"Scientific mode bhi try karein 🔬",
"Perfect! Kuch aur calculate karein? ✨",
"Excellent! 🏆",
"Acha result aaya! 🚀",
"Tip: % button se percentage nikaalein 💡",
"Mathematics mein master ho raho! 🌟"
];
function setTip(msg) {
aiMsgEl.textContent = msg || tips[Math.floor(Math.random() * tips.length)];
}
// ===================================================
// CLOCK
// ===================================================
function updateClock() {
const n = new Date();
document.getElementById('clock').textContent =
String(n.getHours()).padStart(2, '0') + ':' +
String(n.getMinutes()).padStart(2, '0');
}
updateClock();
setInterval(updateClock, 1000);
// ===================================================
// BUTTON HANDLERS
// ===================================================
// Number press (0–9)
function pressNum(n) {
if (justCalced) {
current = n;
prev = '';
operator = '';
justCalced = false;
} else {
current = current === '0' ? n : current + n;
}
updateDisplay();
}
// Operator press (+, -, *, /)
function pressOp(op) {
// Agar pehle se prev + operator hai toh pehle calculate karo
if (prev && operator && !justCalced) {
doCalc(false);
}
prev = current;
operator = op;
current = '0';
justCalced = false;
updateDisplay();
setTip("Operator diya! Ab doosra number daalo aur = dabao 👇");
}
// Decimal point
function pressDot() {
if (justCalced) {
current = '0.';
justCalced = false;
} else if (!current.includes('.')) {
current += '.';
}
updateDisplay();
}
// AC — All Clear
function clearAll() {
current = '0';
prev = '';
operator = '';
justCalced = false;
histEl.textContent = '';
updateDisplay();
setTip("Clear ho gaya! Naya calculation shuru karein 🌟");
}
// Backspace
function backspace() {
if (justCalced) return;
current = current.length <= 1 ? '0' : current.slice(0, -1);
updateDisplay();
}
// Toggle +/−
function toggleSign() {
if (current === '0') return;
current = current.startsWith('-') ? current.slice(1) : '-' + current;
updateDisplay();
}
// Percentage
function percent() {
current = String(parseFloat(current) / 100);
updateDisplay();
setTip("Percentage calculate ho gayi! 💯");
}
// ===================================================
// CORE CALCULATION
// ===================================================
function doCalc(showHistory = true) {
const a = parseFloat(prev);
const b = parseFloat(current);
if (isNaN(a) || isNaN(b) || !operator) return;
let res;
switch (operator) {
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/':
if (b === 0) {
current = 'Error';
prev = '';
operator = '';
updateDisplay();
setTip("Zero se divide nahi hota! ⚠️");
return;
}
res = a / b;
break;
default: return;
}
if (showHistory) {
histEl.textContent = prev + ' ' + opSymbol[operator] + ' ' + current + ' =';
}
// Float precision fix
current = parseFloat(res.toFixed(10)).toString();
prev = '';
operator = '';
justCalced = true;
flashPop();
}
// = button
function calculate() {
if (!prev || !operator) return;
doCalc(true);
updateDisplay();
setTip(tips[Math.floor(Math.random() * tips.length)]);
}
// ===================================================
// SCIENTIFIC FUNCTIONS
// ===================================================
function sciFunc(fn) {
const val = parseFloat(current);
let res, msg;
switch (fn) {
case 'sin':
res = Math.sin(val * Math.PI / 180);
msg = 'sin(' + val + '°) = ' + res.toFixed(6) + ' 📐';
break;
case 'cos':
res = Math.cos(val * Math.PI / 180);
msg = 'cos(' + val + '°) = ' + res.toFixed(6) + ' 📐';
break;
case 'tan':
res = Math.tan(val * Math.PI / 180);
msg = 'tan(' + val + '°) = ' + res.toFixed(6) + ' 📐';
break;
case 'log':
if (val <= 0) { setTip("log sirf positive numbers ke liye hota hai! ⚠️"); return; }
res = Math.log10(val);
msg = 'log(' + val + ') calculate kiya! 🔬';
break;
case 'sqrt':
if (val < 0) { setTip("Negative number ka square root nahi hota! ⚠️"); return; }
res = Math.sqrt(val);
msg = '√' + val + ' = ' + res.toFixed(6) + ' ✨';
break;
case 'sq':
res = val * val;
msg = val + '² = ' + res + ' 💥';
break;
case 'pi':
current = '3.14159265358979';
histEl.textContent = 'π =';
updateDisplay();
setTip("π — ek magical number! 🔵");
return;
case 'euler':
current = '2.71828182845905';
histEl.textContent = "Euler's e =";
updateDisplay();
setTip("e ≈ 2.718 — Euler ka number 🌀");
return;
default:
return;
}
histEl.textContent = fn + '(' + val + ') =';
current = parseFloat(res.toFixed(10)).toString();
justCalced = true;
flashPop();
updateDisplay();
setTip(msg);
}
// ===================================================
// MODE SWITCH (Basic / Scientific / % Tips)
// ===================================================
function setMode(m, btn) {
// Update active tab
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
btn.classList.add('active');
// Show/hide scientific row
document.getElementById('sciRow').classList.toggle('show', m === 'sci');
// Update AI tip
if (m === 'pct') {
setTip("Tip Mode: Koi number likho → % dabao → Discount ya tip milega! 💰");
} else if (m === 'sci') {
setTip("Scientific mode active! sin, cos, log, √ sab available hain 🔬");
} else {
setTip("Basic mode ready hai! Calculate karein ✨");
}
}
// ===================================================
// KEYBOARD SUPPORT
// ===================================================
document.addEventListener('keydown', function(e) {
if (e.key >= '0' && e.key <= '9') pressNum(e.key);
else if (e.key === '.') pressDot();
else if (e.key === '+') pressOp('+');
else if (e.key === '-') pressOp('-');
else if (e.key === '*') pressOp('*');
else if (e.key === '/') { e.preventDefault(); pressOp('/'); }
else if (e.key === 'Enter' || e.key === '=') calculate();
else if (e.key === 'Backspace') backspace();
else if (e.key === 'Escape') clearAll();
});
// ===== INIT =====
updateDisplay();