Check if two elements of a matrix are on the same diagonal or not | Function to check if two letegers are on the same diagonal of the matrix ; Storing indexes of x in I , J ; Storing Indexes of y in P , Q ; Condition to check if the both the elements are in same diagonal of a matrix ; Dimensions of Matrix ; Given Matrix ; Elements to be checked ; Function call
function checkSameDiag ( li , x , y , m , n ) { let I = 0 , J = 0 ; let P = 0 , Q = 0 ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( li [ i ] [ j ] == x ) { I = i ; J = j ; } if ( li [ i ] [ j ] == y ) { P = i ; Q = j ; } } } if ( P - Q == I - J P + Q == I + J ) { document . write ( " " ) ; } else document . write ( " " ) ; } let m = 6 ; let n = 5 ; let li = [ [ 32 , 94 , 99 , 26 , 82 ] , [ 51 , 69 , 52 , 63 , 17 ] , [ 90 , 36 , 88 , 55 , 33 ] , [ 93 , 42 , 73 , 39 , 28 ] , [ 81 , 31 , 83 , 53 , 10 ] , [ 12 , 29 , 85 , 80 , 87 ] ] ; let x = 42 ; let y = 80 ; checkSameDiag ( li , x , y , m , n ) ;