Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created April 17, 2020 00:30
Show Gist options
  • Save RP-3/bb590d67ab0363dbbd82343c6657177d to your computer and use it in GitHub Desktop.
Save RP-3/bb590d67ab0363dbbd82343c6657177d to your computer and use it in GitHub Desktop.
var stringShift = function(s, shift) {
const net = shift.reduce((acc, [direction, magnitude]) => {
return acc + (direction === 1 ? magnitude : -magnitude);
}, 0) % s.length;
if(net === 0) return s;
if(net > 0){
const [left, right] = [s.slice(0, s.length - net), s.slice(s.length - net)];
return right + left;
}else{
const [left, right] = [s.slice(0, Math.abs(net)), s.slice(Math.abs(net))];
return right + left;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment