skip to Main Content

In the following example an array of numbers is shuffled each time the document is refreshed. How do I apply the same function that shuffles the numbers to shuffle the blocks of color?

const points = [1, 2, 3, 4, 5, 6];
document.getElementById("numbers").innerHTML = points;

function myFunction() {
  for (let i = points.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    let k = points[i];
    points[i] = points[j];
    points[j] = k;
  }
  document.getElementById("numbers").innerHTML = points;
}
<!DOCTYPE html>
<html>

<body onload="myFunction()">

  <div id="numbers"></div>

  <br>

  <div id="colors">
    <div style="width:50px; height:50px; background-color:red; display:inline-block"></div>
    <div style="width:50px; height:50px; background-color:yellow; display:inline-block"></div>
    <div style="width:50px; height:50px; background-color:blue; display:inline-block"></div>
    <div style="width:50px; height:50px; background-color:orange; display:inline-block"></div>
    <div style="width:50px; height:50px; background-color:green; display:inline-block"></div>
    <div style="width:50px; height:50px; background-color:purple; display:inline-block"></div>
  </div>

</body>

</html>

2

Answers


  1. Get the div elements in an array, shuffle it, and then append those elements again under the same parent element. The latter action will actually move them:

    function shuffle(arr) { // Use parameter to make it more general purpose
      for (let i = arr.length -1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i+1));
        let k = arr[i];
        arr[i] = arr[j];
        arr[j] = k;
      }
    }
    
    const colors = document.querySelector("#colors");
    const children = [...colors.children]; 
    shuffle(children);
    colors.append(...children);
    <div id="colors">
        <div style="width:50px; height:50px; background-color:red; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:yellow; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:blue; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:orange; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:green; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:purple; display:inline-block"></div>
    </div>
    Login or Signup to reply.
  2. You can apply a shuffle directly to the DIVs. You start with an unshuffled region, and an initially empty shuffled region. You then pick items at random from the unshuffled region to place into the shuffled region.

    let p = document.getElementById('colors')
    let l = p.children.length
    for(let i=l+1; --i;) p.appendChild(p.children.item(Math.random()*i|0))
    <div id="colors">
        <div style="width:50px; height:50px; background-color:red; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:yellow; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:blue; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:orange; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:green; display:inline-block"></div>
        <div style="width:50px; height:50px; background-color:purple; display:inline-block"></div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search