Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script >
var array = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 , 77 , 78 , 79 , 80 , 81 , 82 , 83 , 84 , 85 , 86 , 87 , 88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 ];
Array .prototype .move1 = function (from , to ) {
this .splice (to, 0 , this .splice (from , 1 )[0 ]);
};
Array .prototype .move2 = function (pos1, pos2 ) {
var i, tmp;
pos1 = parseInt (pos1, 10 );
pos2 = parseInt (pos2, 10 );
if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this .length && 0 <= pos2 && pos2 <= this .length ) {
tmp = this [pos1];
if (pos1 < pos2) {
for (i = pos1; i < pos2; i++) {
this [i] = this [i + 1 ];
}
}
else {
for (i = pos1; i > pos2; i--) {
this [i] = this [i - 1 ];
}
}
this [pos2] = tmp;
}
}
Array .prototype .move3 = function (from , to ) {
if (this .length > 100 ) {
return this .move2 (from , to);
} else {
return this .move1 (from , to);
}
};
</script >
Setup JS
Teardown JS