Skip to content

Instantly share code, notes, and snippets.

@fnakstad
Created December 8, 2014 04:28
Show Gist options
  • Save fnakstad/f708026bf9182d55f09d to your computer and use it in GitHub Desktop.
Save fnakstad/f708026bf9182d55f09d to your computer and use it in GitHub Desktop.
Circular shift
// First creates a shallow copy of array,
// then shifts contents circularly according to
// number of steps and direction given
function circularShift(arrayPar, steps, shiftLeft) {
var array = arrayPar.slice(0);
for(var i = 0; i < steps; i++) {
if(shiftLeft)
array.push(array.shift());
else
array.unshift(array.pop());
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment