I have a deck of cards to study Japanese kanji and vocab (specifically for 漢検2級). In several of these problem sets, a list of words is presented for each question that you pick the answer out of and then write the kanji (example). In my cards I wanted to have the list of words be randomized so I wasn’t memorizing the position of elements. I had Claude write the Javascript below to help me with this.
I can’t be the first to run across this issue of wanting to have certain elements on a card be shown in random order. Is there a standard add-on or function available that I’m not aware of that does the same thing?
<script>
(function() {
// 1. Get the element containing the list
var container = document.getElementById('scramble-list');
if (!container) return;
// 2. Read the raw text from the field
var text = container.textContent.trim();
if (!text) return;
// 3. Split by standard US comma (,) OR Japanese comma (、)
// The regex [,\u3001] matches either character
var list = text.split(/[,\u3001]/);
// 4. Clean up whitespace from each item
list = list.map(function(item) {
return item.trim();
}).filter(Boolean); // Removes empty items if there are trailing commas
// 5. Fisher-Yates Shuffle Algorithm to randomize the array
for (var i = list.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
// 6. Join them back together.
// You can change ', ' to '、' below if you prefer Japanese commas in the output.
container.textContent = list.join('、');
})();
</script>