shism commited on
Commit
3eea0c5
·
verified ·
1 Parent(s): 6857ca3

Review the entire code and fix any bugs - Follow Up Deployment

Browse files
Files changed (1) hide show
  1. index.html +91 -9
index.html CHANGED
@@ -23,6 +23,38 @@
23
  background-color: #4f46e5;
24
  border-radius: 3px;
25
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  .symbol-btn {
27
  transition: all 0.2s ease;
28
  }
@@ -73,7 +105,7 @@
73
  placeholder="Enter formula (e.g., 2x + 3 = 7)"
74
  ></textarea>
75
  <div id="result" class="text-2xl font-bold text-gray-800 mt-2 text-right"></div>
76
- <div id="error" class="text-red-500 text-sm mt-1 text-right"></div>
77
  </div>
78
 
79
  <!-- Symbolic Input Buttons -->
@@ -193,8 +225,18 @@
193
  }
194
 
195
  function backspace() {
196
- currentExpression = currentExpression.slice(0, -1);
197
- updateScreen();
 
 
 
 
 
 
 
 
 
 
198
  }
199
 
200
  function updateScreen() {
@@ -292,7 +334,7 @@
292
  .replace(/log\(/g, 'Math.log10(');
293
 
294
  // Handle factorial with gamma function for non-integers and better precision
295
- expr = expr.replace(/([\d.]+)!/g, function(match, num) {
296
  const n = parseFloat(num);
297
  if (n < 0) throw "Factorial of negative number";
298
  if (n > 170) throw "Number too large for factorial";
@@ -323,16 +365,52 @@
323
 
324
  // Sanitize the expression before evaluation
325
  const sanitized = expr.replace(/Math\./g, '');
326
- // Check for invalid variables (only x,y,z allowed)
327
- const invalidVars = sanitized.match(/[a-df-wA-DF-W]/g);
328
  if (invalidVars) {
329
  const uniqueVars = [...new Set(invalidVars)];
330
- throw `Invalid variable${uniqueVars.length > 1 ? 's' : ''}: ${uniqueVars.join(', ')}. Only x, y, z are allowed as variables.`;
331
  }
332
 
333
  // If expression contains variables but no equals sign
334
  if (/[xyz]/.test(sanitized) && !currentExpression.includes('=')) {
335
- throw `Expression contains variables but no equation (missing '='). Did you mean to solve for a variable?`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  }
337
 
338
  // Warn about very large/small numbers before evaluation
@@ -361,7 +439,11 @@
361
  addToHistory(currentExpression, result);
362
 
363
  } catch (error) {
364
- document.getElementById('error').textContent = 'Error: ' + error;
 
 
 
 
365
  document.getElementById('result').textContent = '';
366
  if (error.includes('variables but no equation')) {
367
  document.getElementById('error').textContent += '\nTip: To evaluate expressions with variables, assign them values first (e.g. "x=5")';
 
23
  background-color: #4f46e5;
24
  border-radius: 3px;
25
  }
26
+ .analysis-box {
27
+ background-color: #f8fafc;
28
+ border-left: 4px solid #4f46e5;
29
+ padding: 12px;
30
+ margin-top: 8px;
31
+ border-radius: 0 4px 4px 0;
32
+ font-family: monospace;
33
+ white-space: pre-wrap;
34
+ color: #334155;
35
+ }
36
+ .analysis-title {
37
+ font-weight: bold;
38
+ color: #4f46e5;
39
+ margin-bottom: 6px;
40
+ }
41
+ .analysis-item {
42
+ margin-left: 12px;
43
+ position: relative;
44
+ }
45
+ .analysis-item:before {
46
+ content: "•";
47
+ position: absolute;
48
+ left: -12px;
49
+ color: #4f46e5;
50
+ }
51
+ .analysis-tip {
52
+ background-color: #e0e7ff;
53
+ padding: 8px;
54
+ border-radius: 4px;
55
+ margin-top: 8px;
56
+ font-style: italic;
57
+ }
58
  .symbol-btn {
59
  transition: all 0.2s ease;
60
  }
 
105
  placeholder="Enter formula (e.g., 2x + 3 = 7)"
106
  ></textarea>
107
  <div id="result" class="text-2xl font-bold text-gray-800 mt-2 text-right"></div>
108
+ <div id="error" class="text-sm mt-1"></div>
109
  </div>
110
 
111
  <!-- Symbolic Input Buttons -->
 
225
  }
226
 
227
  function backspace() {
228
+ const input = document.getElementById('formulaInput');
229
+ const startPos = input.selectionStart;
230
+ const endPos = input.selectionEnd;
231
+
232
+ if (startPos === endPos && startPos > 0) {
233
+ input.value = input.value.substring(0, startPos - 1) + input.value.substring(endPos);
234
+ input.setSelectionRange(startPos - 1, startPos - 1);
235
+ } else {
236
+ input.value = input.value.substring(0, startPos) + input.value.substring(endPos);
237
+ input.setSelectionRange(startPos, startPos);
238
+ }
239
+ input.focus();
240
  }
241
 
242
  function updateScreen() {
 
334
  .replace(/log\(/g, 'Math.log10(');
335
 
336
  // Handle factorial with gamma function for non-integers and better precision
337
+ expr = expr.replace(/([+-]?[\d.]+)!/g, function(match, num) {
338
  const n = parseFloat(num);
339
  if (n < 0) throw "Factorial of negative number";
340
  if (n > 170) throw "Number too large for factorial";
 
365
 
366
  // Sanitize the expression before evaluation
367
  const sanitized = expr.replace(/Math\./g, '');
368
+ // Check for invalid variables (allow any single letter variables)
369
+ const invalidVars = sanitized.match(/[^a-zA-Z\s\d\.\+\-\*\/\^\(\)\,]/g);
370
  if (invalidVars) {
371
  const uniqueVars = [...new Set(invalidVars)];
372
+ throw `Invalid character${uniqueVars.length > 1 ? 's' : ''}: ${uniqueVars.join(', ')}`;
373
  }
374
 
375
  // If expression contains variables but no equals sign
376
  if (/[xyz]/.test(sanitized) && !currentExpression.includes('=')) {
377
+ const vars = [...new Set(sanitized.match(/[xyz]/g))];
378
+ const varList = vars.join(', ');
379
+ // Special case for simple linear expressions like "2x+3"
380
+ if (vars.length === 1 && /^[+-]?\d*[xyz]([+-]\d+)?$/.test(sanitized.replace(/\s/g, ''))) {
381
+ const match = sanitized.match(/^([+-]?\d*)([xyz])([+-]\d+)?$/);
382
+ const coeff = match[1] ? (match[1] === '+' ? 1 : match[1] === '-' ? -1 : parseFloat(match[1])) : 1;
383
+ const varName = match[2];
384
+ const constant = match[3] ? parseFloat(match[3]) : 0;
385
+
386
+ const analysis = `<div class="analysis-box">
387
+ <div class="analysis-title">Linear Expression Analysis</div>
388
+ <div class="analysis-item">Form: <span class="font-mono">${coeff !== 1 ? coeff : ''}${varName} ${constant >= 0 ? '+' : ''}${constant !== 0 ? constant : ''}</span></div>
389
+ <div class="analysis-item">Slope (coefficient of ${varName}): <span class="font-bold">${coeff}</span></div>
390
+ <div class="analysis-item">Y-intercept: <span class="font-bold">${constant}</span></div>
391
+
392
+ <div class="analysis-tip">
393
+ <div class="font-bold mb-1">To evaluate:</div>
394
+ <div>1. Assign value to ${varName} (e.g. <span class="font-mono bg-gray-100 px-1">${varName}=5</span>)</div>
395
+ <div>2. Or make equation to solve (e.g. <span class="font-mono bg-gray-100 px-1">${currentExpression}=7</span>)</div>
396
+ </div>
397
+ </div>`;
398
+ throw analysis;
399
+ } else {
400
+ const analysis = `<div class="analysis-box">
401
+ <div class="analysis-title">Expression Analysis</div>
402
+ <div class="analysis-item">Contains variable${vars.length > 1 ? 's' : ''}: <span class="font-bold">${varList}</span></div>
403
+ <div class="analysis-item">Not an equation (missing '=')</div>
404
+
405
+ <div class="analysis-tip">
406
+ <div class="font-bold mb-1">To evaluate this expression:</div>
407
+ <div>1. Assign values to variables (e.g. <span class="font-mono bg-gray-100 px-1">${vars[0]}=5</span>)</div>
408
+ <div>2. Or make it an equation (e.g. <span class="font-mono bg-gray-100 px-1">${currentExpression}=7</span>)</div>
409
+ </div>
410
+ </div>`;
411
+ throw analysis;
412
+ }
413
+ throw analysis;
414
  }
415
 
416
  // Warn about very large/small numbers before evaluation
 
439
  addToHistory(currentExpression, result);
440
 
441
  } catch (error) {
442
+ if (typeof error === 'string' && error.startsWith('<div')) {
443
+ document.getElementById('error').innerHTML = error;
444
+ } else {
445
+ document.getElementById('error').textContent = 'Error: ' + error;
446
+ }
447
  document.getElementById('result').textContent = '';
448
  if (error.includes('variables but no equation')) {
449
  document.getElementById('error').textContent += '\nTip: To evaluate expressions with variables, assign them values first (e.g. "x=5")';