title: Mutation of array
body: <p>For some reason I can't get below code working as expected.</p>
  
  <p>I have declared variable dna with random 15 elements, each of its elements is random letter from array dnaBases.</p>
  
  <p>Mutate function should create new (similar array) with random 15 elements, but elements from first array must not repeat. Instead, they should be replaced by the remaining three elements in dnaBases array.</p>
  
  <p>Problem in my code is that sometimes letters are repeating even though in previous cases the did not.</p>
  
  <pre><code>const dnaBases = ['A', 'T', 'C', 'G']
  var dna = []
  for (let i = 0; i &lt; 15; i++) {
    dna.push(dnaBases[Math.floor(Math.random() * 4)])
  }
  
  function mutate() {
    console.log(dna) // to check newly generated dna array
    var tmp = []
    var newDnaBases = ['A', 'T', 'C', 'G']
    for (var j = 0; j &lt; 15; j++) {
      tmp.push(newDnaBases[Math.floor(Math.random() * 4)]);
    }
  
    console.log(tmp) // to check newly generated tmp array
  
    for (var k = 0; k &lt; tmp.length; k++) {
        var randomIndex = Math.floor(Math.random() * 3);
        if (tmp[k] === dna[k]) {
          var x = newDnaBases.splice(tmp[k], 1);
          tmp[k] = newDnaBases[randomIndex];
          newDnaBases.push(x.toString());
        }
    }
    console.log(tmp) // to see how tmp has changed after for loop
    console.log(newDnaBases) // to check if newDnaBases is not corrupted
  }
  
  mutate()
  </code></pre>
  
  <p>I am new in Javascript and I can't see the problem at first glance.</p>
  
  <p>Thanks a lot!</p>