Last active
May 16, 2021 15:25
-
-
Save tobi-bams/9e054384dabd473b08d72d886ece1bb9 to your computer and use it in GitHub Desktop.
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
const shuffleClass = (arr, num) => { | |
if (!Array.isArray(arr) || arr.length === 0) return []; | |
if (typeof num !== "number") { | |
return arr; | |
} | |
if (num === 0) { | |
return arr; | |
} | |
if (num >= 1) { | |
num = Math.round(num); | |
if (num > arr.length) { | |
num = num % arr.length; | |
} | |
for (let i = 0; i < arr.length - num; i++) { | |
let removedItem = arr.shift(); | |
arr.push(removedItem); | |
} | |
} | |
if (num < 0) { | |
num = Math.abs(num); | |
num = Math.round(num); | |
if (num > arr.length) { | |
num = num % arr.length; | |
} | |
for (let i = 0; i < num; i++) { | |
let removedItem = arr.shift(); | |
arr.push(removedItem); | |
} | |
} | |
return arr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment