Skip to content

Commit 61b1c7a

Browse files
Fix: Add dimension validation and result reset logic to Matrix Calculator
1 parent 4fd9742 commit 61b1c7a

1 file changed

Lines changed: 81 additions & 13 deletions

File tree

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

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,76 @@ function initMatrixCalculator() {
501501
} else {
502502
document.getElementById('matrixBDimensions').style.opacity = '1';
503503
}
504+
505+
// Reset result display to default when operation changes
506+
if (resultDiv) {
507+
resultDiv.innerHTML = 'Select an operation and click Calculate';
508+
}
504509
}
505510

506-
// Apply dimensions and create grids
507-
function applyDimensions() {
511+
// Validate a single dimension value; returns an error string or null if valid
512+
function validateDimension(value, label) {
513+
if (value === null || value === undefined || (typeof value === 'string' && value.trim() === '')) {
514+
return `${label} is empty. Please enter a value between 1 and 5.`;
515+
}
516+
const num = Number(value);
517+
if (isNaN(num)) {
518+
return `${label} is not a valid number.`;
519+
}
520+
if (!Number.isInteger(num)) {
521+
return `${label} must be a whole number (got ${value}).`;
522+
}
523+
if (num < 1 || num > 5) {
524+
return `${label} must be between 1 and 5 (got ${num}).`;
525+
}
526+
return null;
527+
}
528+
529+
// Validate all required dimension inputs; returns { valid, aRows, aCols, bRows, bCols, error }
530+
function validateDimensions() {
531+
const operation = operationSelect.value;
532+
const needsB = ['add', 'subtract', 'multiply'].includes(operation);
533+
534+
const checks = [
535+
{ value: matrixARows.value, label: 'Matrix A Rows' },
536+
{ value: matrixACols.value, label: 'Matrix A Cols' },
537+
];
538+
539+
if (needsB) {
540+
checks.push(
541+
{ value: matrixBRows.value, label: 'Matrix B Rows' },
542+
{ value: matrixBCols.value, label: 'Matrix B Cols' },
543+
);
544+
}
545+
546+
for (const { value, label } of checks) {
547+
const error = validateDimension(value, label);
548+
if (error) {
549+
return { valid: false, error };
550+
}
551+
}
552+
508553
const aRows = parseInt(matrixARows.value);
509554
const aCols = parseInt(matrixACols.value);
510-
const bRows = parseInt(matrixBRows.value);
511-
const bCols = parseInt(matrixBCols.value);
512-
555+
const bRows = needsB ? parseInt(matrixBRows.value) : 2;
556+
const bCols = needsB ? parseInt(matrixBCols.value) : 2;
557+
558+
return { valid: true, aRows, aCols, bRows, bCols, error: null };
559+
}
560+
561+
// Apply dimensions and create grids
562+
function applyDimensions() {
563+
const dims = validateDimensions();
564+
if (!dims.valid) {
565+
resultDiv.innerHTML = `<span style="color: #ef4444;">⚠️ ${dims.error}</span>`;
566+
return;
567+
}
568+
569+
const { aRows, aCols, bRows, bCols } = dims;
570+
571+
// Reset result display to default when dimensions are successfully applied
572+
resultDiv.innerHTML = 'Select an operation and click Calculate';
573+
513574
// Initialize matrices with zeros
514575
matrixA = Array(aRows).fill().map(() => Array(aCols).fill(0));
515576
matrixB = Array(bRows).fill().map(() => Array(bCols).fill(0));
@@ -523,16 +584,23 @@ function initMatrixCalculator() {
523584
// Perform calculation
524585
function calculate() {
525586
try {
526-
// Get current values from grids
527-
const aRows = parseInt(matrixARows.value);
528-
const aCols = parseInt(matrixACols.value);
529-
const bRows = parseInt(matrixBRows.value);
530-
const bCols = parseInt(matrixBCols.value);
531-
587+
// Validate dimensions before reading matrix values
588+
const dims = validateDimensions();
589+
if (!dims.valid) {
590+
resultDiv.innerHTML = `<span style="color: #ef4444;">⚠️ ${dims.error}</span>`;
591+
return;
592+
}
593+
594+
const { aRows, aCols, bRows, bCols } = dims;
595+
596+
const operation = operationSelect.value;
597+
const needsB = ['add', 'subtract', 'multiply'].includes(operation);
598+
532599
matrixA = getMatrixValues(aRows, aCols, matrixAGrid);
533-
matrixB = getMatrixValues(bRows, bCols, matrixBGrid);
600+
if (needsB) {
601+
matrixB = getMatrixValues(bRows, bCols, matrixBGrid);
602+
}
534603

535-
const operation = operationSelect.value;
536604
let result;
537605
let operationName = '';
538606

0 commit comments

Comments
 (0)