Fisher-Yates Shuffle

The most common way to shuffle an array of things. Based on the Wikipedia article.

This is a part of a larger program where you can create an HTML list and then randomize their order.

$shuffleBtn.addEventListener('click', e => {
  const $items = Array.from($list.querySelectorAll('li'));
  
  const n = $items.length;
  for (let i = n-1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i+1));
    [$items[j], $items[i]] = [$items[i], $items[j]];
  }
  
  for (const $item of $items) {
    $list.appendChild($item);
  }
});