Skip to content

Commit 3dd608b

Browse files
Fix: Address Copilot review comments on validation and duplication
1 parent 61b1c7a commit 3dd608b

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

web-app/js/projects/matrix-calculator.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,15 @@ function initMatrixCalculator() {
488488
return inverse;
489489
}
490490

491+
// Helper: Check if operation requires Matrix B
492+
function operationNeedsB(operation) {
493+
return ['add', 'subtract', 'multiply'].includes(operation);
494+
}
495+
491496
// Update Matrix B visibility based on operation
492497
function updateMatrixBVisibility() {
493498
const operation = operationSelect.value;
494-
const needsB = ['add', 'subtract', 'multiply'].includes(operation);
499+
const needsB = operationNeedsB(operation);
495500
matrixBPanel.style.display = needsB ? 'block' : 'none';
496501

497502
// For transpose, determinant, rank, inverse, we only need matrix A dimensions
@@ -529,7 +534,7 @@ function initMatrixCalculator() {
529534
// Validate all required dimension inputs; returns { valid, aRows, aCols, bRows, bCols, error }
530535
function validateDimensions() {
531536
const operation = operationSelect.value;
532-
const needsB = ['add', 'subtract', 'multiply'].includes(operation);
537+
const needsB = operationNeedsB(operation);
533538

534539
const checks = [
535540
{ value: matrixARows.value, label: 'Matrix A Rows' },
@@ -550,10 +555,10 @@ function initMatrixCalculator() {
550555
}
551556
}
552557

553-
const aRows = parseInt(matrixARows.value);
554-
const aCols = parseInt(matrixACols.value);
555-
const bRows = needsB ? parseInt(matrixBRows.value) : 2;
556-
const bCols = needsB ? parseInt(matrixBCols.value) : 2;
558+
const aRows = parseInt(matrixARows.value, 10);
559+
const aCols = parseInt(matrixACols.value, 10);
560+
const bRows = needsB ? parseInt(matrixBRows.value, 10) : 2;
561+
const bCols = needsB ? parseInt(matrixBCols.value, 10) : 2;
557562

558563
return { valid: true, aRows, aCols, bRows, bCols, error: null };
559564
}
@@ -562,7 +567,11 @@ function initMatrixCalculator() {
562567
function applyDimensions() {
563568
const dims = validateDimensions();
564569
if (!dims.valid) {
565-
resultDiv.innerHTML = `<span style="color: #ef4444;">⚠️ ${dims.error}</span>`;
570+
resultDiv.innerHTML = '';
571+
const warning = document.createElement('span');
572+
warning.style.color = '#ef4444';
573+
warning.textContent = `⚠️ ${dims.error}`;
574+
resultDiv.appendChild(warning);
566575
return;
567576
}
568577

@@ -587,14 +596,18 @@ function initMatrixCalculator() {
587596
// Validate dimensions before reading matrix values
588597
const dims = validateDimensions();
589598
if (!dims.valid) {
590-
resultDiv.innerHTML = `<span style="color: #ef4444;">⚠️ ${dims.error}</span>`;
599+
resultDiv.innerHTML = '';
600+
const warning = document.createElement('span');
601+
warning.style.color = '#ef4444';
602+
warning.textContent = `⚠️ ${dims.error}`;
603+
resultDiv.appendChild(warning);
591604
return;
592605
}
593606

594607
const { aRows, aCols, bRows, bCols } = dims;
595608

596609
const operation = operationSelect.value;
597-
const needsB = ['add', 'subtract', 'multiply'].includes(operation);
610+
const needsB = operationNeedsB(operation);
598611

599612
matrixA = getMatrixValues(aRows, aCols, matrixAGrid);
600613
if (needsB) {

0 commit comments

Comments
 (0)