title: Shuffle arrays of strings in JavaScript with repeated elements that are at least two elements apart
body: <p>I have two arrays of strings:</p>
  <pre><code>var array1 = [&quot;word1&quot;, &quot;word2&quot;, &quot;word3&quot;, &quot;word4&quot;, &quot;word5&quot;, &quot;word6&quot;];
  var array2 = [&quot;word7&quot;, &quot;word8&quot;];
  </code></pre>
  <p>I need to concatenate these arrays into a new array and shuffle the elements in a random order. However, the elements from <code>array2</code> should repeat appear twice in this new array, and the repeats of a given element must be at least two elements apart from each other.</p>
  <p>Here's an example result that satisfies these conditions:</p>
  <pre><code>[&quot;word7&quot;, &quot;word3&quot;, &quot;word8&quot;, &quot;word7&quot;, &quot;word1&quot;, &quot;word4&quot;, &quot;word6&quot;, &quot;word8&quot;, &quot;word5&quot;, &quot;word2&quot;];
  </code></pre>
  <p>All the elements are in a random order, and the repeated elements have at least two other elements between them. How can I write a function (using no extra libraries, preferably) that creates a randomly ordered array that satisfies the above conditions? I've tried extending standard permutation algorithms (e.g. Fisher-Yates) but I'm tripping over the implementation as I'm not very familiar with JS.</p>
  <p>Any help greatly appreciated - thanks!</p>