X



Home>Tutorial>

Fastest 2x2 array matrix rotation using javascript

Admin Wednesday, July 15, 2020 JavaScript

There was a time I needed to rotate 2D dimension array from right to left and after so much searching here and there, a taught finally came to my mind which is javascript reverse() and the push () functions and these two solved my problem perfectly. So therefore, in this article, I will show you a trick to rotate javascript array in reverse order. Maybe there is a better way to do this but I think this is simple if am on the right track else you can let me know on the comment section.
<html>
<head>
<title></title>
<script>
function rotate (){
var grid=[[0,1],[0,2]];
var g;
for(i=0;i<grid.length;i++)
{
g=grid[i];
g=g.reverse();
var f= new Array();
f.push(g);
document.write("<br>" + f);
}
}
</script>
</head>
<body onload="rotate();">
</body>
</html>

<html>
<head>
<title></title>
<script>
function rotate (){
var grid=[[0,1],[0,2]];
var g;
for(i=0;i<grid.length;i++)
{
g=grid[i];
g=g.reverse();
var f= new Array();
f.push(g);
document.write("<br>" + f);
}
}
</script>
</head>
<body onload="rotate();">
</body>
</html>

| | Views(765)
Ejeh SaysFriday, July 24, 2020
Good one

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services