Created
April 26, 2016 04:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you please explain the code why you have used arr[0].length