A 4 * 4 matrix has 7 diagonals . Hence ( 2 * n - 1 ) iterations
for ( let t = 0 ; t < ( 2 * n - 1 ) ; t ++ ) { let t1 = t ; if ( t1 >= n ) { mode ++ ; t1 = n - 1 ; it -- ; lower ++ ; } else { lower = 0 ; it ++ ; } for ( let i = t1 ; i >= lower ; i -- ) { if ( ( t1 + mode ) % 2 === 0 ) { console . log ( arr [ i ] [ t1 + lower - i ] ) } else { console . log ( arr [ t1 + lower - i ] [ i ] ) } } } }