Created
December 8, 2014 04:28
-
-
Save fnakstad/f708026bf9182d55f09d to your computer and use it in GitHub Desktop.
Circular shift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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