JSFiddle - React, Tailwind, and code Playground
by joeycakes
HTML
<pre id="output"></pre>
JavaScript
Array.prototype.move = function (old_index, new_index) {
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
return this; // for testing purposes
};
function write (text) {
document.getElementById("output").appendChild(
document.createTextNode(text)
);
}
function writeln (text) {
write(text + "\n");
}
var moves = 0,
arr = [],
len = 52,
step = 6,
stepx = (len / step) - 1;
while (len--) {
arr[len] = len + 1;
}
len = arr.length;
writeln("Starting array: " + arr);
writeln("Step: " + step);
writeln("StepX: " + stepx);
var x, y, i, items, newarr = [];
for (x = 0; x < stepx; x++) {
items = arr.splice(0, step);
while (items.length > 0) {
newarr.unshift(items.pop());
}
moves++;
}
while (arr.length > 0) {
newarr.unshift(arr.pop());
}
arr = newarr;
writeln("Updated array: " + arr);
for (x = 0; x < stepx; x++) {
i = ((x + 1) * step - 1);
// for 3x
//arr.move(i, i - 2);
//moves++;
//arr.move(i, i - 1);
//moves++;
// for 4x -fails need extra itteration
//arr.move(i-1, i - 4);
//moves++;
//arr.move(i-1, i - 3);
//moves++;
//arr.move(i-1, i - 2);
//moves++;
// for 5x
//arr.move(i, i - 2);
//moves++;
//arr.move(i, i - 1);
//moves++;
writeln(arr[i]);
}
writeln("Updated array: " + arr);
writeln("Moves: " + moves);