Skip to content

Instantly share code, notes, and snippets.

@matdave
Created October 8, 2021 15:32
Show Gist options
  • Save matdave/fbfc5f84bb3ed01d519fa354abfb1181 to your computer and use it in GitHub Desktop.
Save matdave/fbfc5f84bb3ed01d519fa354abfb1181 to your computer and use it in GitHub Desktop.
simple JS randomizer commands
const showrandom = document.querySelectorAll("[showrandom]");
showrandom.forEach(el=>{
const count = el.getAttribute('showrandom') ? el.getAttribute('showrandom') : 1;
const children = el.children;
while(children.length > count){
const random = Math.floor(Math.random() * children.length);
children[random].remove();
}
});
const shuffle = document.querySelectorAll("[shuffle]");
shuffle.forEach(el=>{
const children = el.children;
let newChildren = [];
while(children.length > 0){
const random = Math.floor(Math.random() * children.length);
newChildren.push(children[random]);
children[random].remove();
}
newChildren.forEach(child => {
el.append(child);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment