Skip to content

Instantly share code, notes, and snippets.

@Adobe-Android
Created August 2, 2018 20:13
Show Gist options
  • Save Adobe-Android/70713eb5532e65289b934c6bddf30fbc to your computer and use it in GitHub Desktop.
Save Adobe-Android/70713eb5532e65289b934c6bddf30fbc to your computer and use it in GitHub Desktop.
A code snippet that rotates all elements in an array n many times.
let firstArr = [
1,
2,
3,
4,
5
]
// shift 1: 23451
// shift 2: 34512
// shift 3: 45123
// shift 4: 51234
// n is the number of times to shift
function shift(n) {
for (let i = 0; i < n; i++) {
let firstEl = firstArr.shift();
firstArr.push(firstEl);
}
console.log(firstArr);
}
shift(4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment