@@ -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
@@ -501,15 +506,80 @@ function initMatrixCalculator() {
501506 } else {
502507 document . getElementById ( 'matrixBDimensions' ) . style . opacity = '1' ;
503508 }
509+
510+ // Reset result display to default when operation changes
511+ if ( resultDiv ) {
512+ resultDiv . innerHTML = 'Select an operation and click Calculate' ;
513+ }
514+ }
515+
516+ // Validate a single dimension value; returns an error string or null if valid
517+ function validateDimension ( value , label ) {
518+ if ( value === null || value === undefined || ( typeof value === 'string' && value . trim ( ) === '' ) ) {
519+ return `${ label } is empty. Please enter a value between 1 and 5.` ;
520+ }
521+ const num = Number ( value ) ;
522+ if ( isNaN ( num ) ) {
523+ return `${ label } is not a valid number.` ;
524+ }
525+ if ( ! Number . isInteger ( num ) ) {
526+ return `${ label } must be a whole number (got ${ value } ).` ;
527+ }
528+ if ( num < 1 || num > 5 ) {
529+ return `${ label } must be between 1 and 5 (got ${ num } ).` ;
530+ }
531+ return null ;
532+ }
533+
534+ // Validate all required dimension inputs; returns { valid, aRows, aCols, bRows, bCols, error }
535+ function validateDimensions ( ) {
536+ const operation = operationSelect . value ;
537+ const needsB = operationNeedsB ( operation ) ;
538+
539+ const checks = [
540+ { value : matrixARows . value , label : 'Matrix A Rows' } ,
541+ { value : matrixACols . value , label : 'Matrix A Cols' } ,
542+ ] ;
543+
544+ if ( needsB ) {
545+ checks . push (
546+ { value : matrixBRows . value , label : 'Matrix B Rows' } ,
547+ { value : matrixBCols . value , label : 'Matrix B Cols' } ,
548+ ) ;
549+ }
550+
551+ for ( const { value, label } of checks ) {
552+ const error = validateDimension ( value , label ) ;
553+ if ( error ) {
554+ return { valid : false , error } ;
555+ }
556+ }
557+
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 ;
562+
563+ return { valid : true , aRows, aCols, bRows, bCols, error : null } ;
504564 }
505565
506566 // Apply dimensions and create grids
507567 function applyDimensions ( ) {
508- const aRows = parseInt ( matrixARows . value ) ;
509- const aCols = parseInt ( matrixACols . value ) ;
510- const bRows = parseInt ( matrixBRows . value ) ;
511- const bCols = parseInt ( matrixBCols . value ) ;
512-
568+ const dims = validateDimensions ( ) ;
569+ if ( ! dims . valid ) {
570+ resultDiv . innerHTML = '' ;
571+ const warning = document . createElement ( 'span' ) ;
572+ warning . style . color = '#ef4444' ;
573+ warning . textContent = `⚠️ ${ dims . error } ` ;
574+ resultDiv . appendChild ( warning ) ;
575+ return ;
576+ }
577+
578+ const { aRows, aCols, bRows, bCols } = dims ;
579+
580+ // Reset result display to default when dimensions are successfully applied
581+ resultDiv . innerHTML = 'Select an operation and click Calculate' ;
582+
513583 // Initialize matrices with zeros
514584 matrixA = Array ( aRows ) . fill ( ) . map ( ( ) => Array ( aCols ) . fill ( 0 ) ) ;
515585 matrixB = Array ( bRows ) . fill ( ) . map ( ( ) => Array ( bCols ) . fill ( 0 ) ) ;
@@ -523,16 +593,27 @@ function initMatrixCalculator() {
523593 // Perform calculation
524594 function calculate ( ) {
525595 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-
596+ // Validate dimensions before reading matrix values
597+ const dims = validateDimensions ( ) ;
598+ if ( ! dims . valid ) {
599+ resultDiv . innerHTML = '' ;
600+ const warning = document . createElement ( 'span' ) ;
601+ warning . style . color = '#ef4444' ;
602+ warning . textContent = `⚠️ ${ dims . error } ` ;
603+ resultDiv . appendChild ( warning ) ;
604+ return ;
605+ }
606+
607+ const { aRows, aCols, bRows, bCols } = dims ;
608+
609+ const operation = operationSelect . value ;
610+ const needsB = operationNeedsB ( operation ) ;
611+
532612 matrixA = getMatrixValues ( aRows , aCols , matrixAGrid ) ;
533- matrixB = getMatrixValues ( bRows , bCols , matrixBGrid ) ;
613+ if ( needsB ) {
614+ matrixB = getMatrixValues ( bRows , bCols , matrixBGrid ) ;
615+ }
534616
535- const operation = operationSelect . value ;
536617 let result ;
537618 let operationName = '' ;
538619
0 commit comments