Skip to main content
copy/paste from fiddle missed len declaration
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

Sometimes it is a good idea to abstract this sort of problem. In many languages doing array-based shifts is very expensive.

Do you need to rotate the array? Why not just virtually 'rotate' your pointer....

for (int turn = 0; turn < 10; turn++) {

    console.log("First player is " + players[(turn + 0) % players.length]);
    console.log("Last player is " + players[(turn + players.length - 1) % players.length]);

}

Alternatively, if you need to create the full array for other reasons, consider the slice and concat:

for (turn = 0; turn < 10; turn++) {
    var offset = turn % len;players.length;
    console.log(offset);
    var playturn = players.slice(offset).concat(players.slice(0, offset));
    console.log(playturn.join(", "));

}

Sometimes it is a good idea to abstract this sort of problem. In many languages doing array-based shifts is very expensive.

Do you need to rotate the array? Why not just virtually 'rotate' your pointer....

for (int turn = 0; turn < 10; turn++) {

    console.log("First player is " + players[(turn + 0) % players.length]);
    console.log("Last player is " + players[(turn + players.length - 1) % players.length]);

}

Alternatively, if you need to create the full array for other reasons, consider the slice and concat:

for (turn = 0; turn < 10; turn++) {
    var offset = turn % len;
    console.log(offset);
    var playturn = players.slice(offset).concat(players.slice(0, offset));
    console.log(playturn.join(", "));

}

Sometimes it is a good idea to abstract this sort of problem. In many languages doing array-based shifts is very expensive.

Do you need to rotate the array? Why not just virtually 'rotate' your pointer....

for (int turn = 0; turn < 10; turn++) {

    console.log("First player is " + players[(turn + 0) % players.length]);
    console.log("Last player is " + players[(turn + players.length - 1) % players.length]);

}

Alternatively, if you need to create the full array for other reasons, consider the slice and concat:

for (turn = 0; turn < 10; turn++) {
    var offset = turn % players.length;
    console.log(offset);
    var playturn = players.slice(offset).concat(players.slice(0, offset));
    console.log(playturn.join(", "));

}
Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

Sometimes it is a good idea to abstract this sort of problem. In many languages doing array-based shifts is very expensive.

Do you need to rotate the array? Why not just virtually 'rotate' your pointer....

for (int turn = 0; turn < 10; turn++) {

    console.log("First player is " + players[(turn + 0) % players.length]);
    console.log("Last player is " + players[(turn + players.length - 1) % players.length]);

}

Alternatively, if you need to create the full array for other reasons, consider the slice and concat:

for (turn = 0; turn < 10; turn++) {
    var offset = turn % len;
    console.log(offset);
    var playturn = players.slice(offset).concat(players.slice(0, offset));
    console.log(playturn.join(", "));

}