Created
August 2, 2018 20:13
-
-
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.
This file contains hidden or 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
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