Skip to content

Instantly share code, notes, and snippets.

@tobi-bams
Last active May 16, 2021 15:25
Show Gist options
  • Save tobi-bams/9e054384dabd473b08d72d886ece1bb9 to your computer and use it in GitHub Desktop.
Save tobi-bams/9e054384dabd473b08d72d886ece1bb9 to your computer and use it in GitHub Desktop.
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