Created
October 8, 2021 15:32
-
-
Save matdave/fbfc5f84bb3ed01d519fa354abfb1181 to your computer and use it in GitHub Desktop.
simple JS randomizer commands
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 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(); | |
} | |
}); |
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 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