# Rotating a 2-D (transposing a matrix) in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

## Transpose:

The transpose of a matrix (2-D array) is simply a flipped version of the original matrix (2-D array). We can transpose a matrix (2-D array) by switching its rows with its columns.

## Example

The code for this will be −

const arr = [
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
];
const transpose = arr => {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < i; j++) {
const tmp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = tmp;
};
}
}
transpose(arr);
console.log(arr);

## Output

The output in the console −

[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
Published on 15-Oct-2020 09:18:44