Skip to content

Instantly share code, notes, and snippets.

@namklabs
Created April 26, 2016 04:23
Show Gist options
  • Save namklabs/bc317cf6b847cc917e215d90cd98d4d0 to your computer and use it in GitHub Desktop.
Save namklabs/bc317cf6b847cc917e215d90cd98d4d0 to your computer and use it in GitHub Desktop.
rotate a 2D (2 dimensional) array clockwise (right) by 90 degrees in JavaScript
function rotateArrayRight( arr ){
// rotates a 2D array 90 degrees to the right (clockwise)
var newarr = [];
for( var x = 0; x < arr[0].length; x++ ){
newarr[x] = [];
for( var y = arr.length - 1; y >= 0; y-- ){
newarr[x].push( arr[y][x] );
}
}
return newarr;
}
@nishanthreddy114
Copy link

can you please explain the code why you have used arr[0].length

@Midoukh
Copy link

Midoukh commented Feb 4, 2022

@nishanthreddy114 to get the length of the first row

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment