Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Given matrix size ; Function call
function UniqueMatrix ( N ) { let element_value = 1 ; let i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( let f = element_value ; f < element_value + N ; f ++ ) { document . write ( f + " " ) ; } element_value += N ; } else { for ( let k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { document . write ( k + " " ) ; } element_value += N ; } document . write ( " " ) ; i = i + 1 ; } } let N = 4 ; UniqueMatrix ( N ) ;