I try to write a function which can rotate the array to the left,eg 1,2,3,4,5,6,7,8,9 and turn to 4,5,6,7,8,9,1,2,3. At first place, I use this funciton below ,but the result is 4,2,3,7,5,6,1,8,9. Therefore, I think it does not drop out of the loop because it only executes once in the loop. Please anyone could help me with this? Any help would be appreciated! Thanks in advance.
var a =[1,2,3,4,5,6,7,8,9];
var len =a.length;
for (i=0;i<3;i++ )
{
var b = a[i];
var j = i;
for(k=0;k<a.length;k++)
{
j+=3;
if(j<a.length)
{
a[i] = a[j];
i=j;
}
else {
j-=3;
a[j]=b;
break;
}
}
}
console.log(a);
If you want algorithm with no array methods you can do it like this :