title: Scramble String According to Array Values - Javascript
body: <p>Trying to solve <a href="https://www.codewars.com/kata/string-scramble/train/javascript" rel="nofollow noreferrer">this question</a> on Codewars.</p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array">other articles</a> that deal with shuffling / scrambling a string randomly. </p>
  
  <p>But what about scrambling a string according to the values in a given array? </p>
  
  <p>I.e. <code>abcd</code> given the array <code>[0, 3, 2, 1]</code> will become <code>acdb</code> because:</p>
  
  <ul>
  <li><code>a</code> moves to index <code>0</code></li>
  <li><code>b</code> moves to index <code>3</code></li>
  <li><code>c</code> moves to index <code>2</code></li>
  <li><code>d</code> moves to index <code>1</code> </li>
  </ul>
  
  <p>My guess is to start out by splitting the string into an array. And then we want to get the index value of the array that's passed into the <code>scramble</code> function, and push the character at the index value from that array into the new array. And finally join the array:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function scramble(str, arr) {
  
    let newArray = str.split("");
    let finalArray = [];
  
    for (let i = 0; i &lt; str.length; i++) {
      console.log(newArray);
      finalArray.push(newArray.splice(arr[i], 1));
    }
    return finalArray;
  }
  
  console.log(scramble("abcd", [0, 3, 1, 2]));</code></pre>
  </div>
  </div>
  </p>
  
  <p>But the problem with this logic is that <code>.splice()</code> removes the character from the <code>newArray</code> every time.</p>
  
  <p>Is there another method that will remove the character at the specified index without modifying the original array?</p>
  
  <p>I don't think slice will work either. </p>