title: How to scramble a word, keeping first and last characters the same? (JS)
body: <p>I have a task, which is to scramble a single word, whose size is greater than 3 letters. </p>
  
  <p>The scrambled word must not be equal to the original, and the first and last letters of the word must remain the same.</p>
  
  <p>For example, the word <code>stack</code>, could give the one of the following results:</p>
  
  <ul>
  <li>satck</li>
  <li>scatk</li>
  <li>stcak </li>
  <li>sactk</li>
  <li>etc</li>
  </ul>
  
  <p>While words like <code>is</code> or <code>hey</code> for example, would remain unchanged for being too small.</p>
  
  <p>My attempt to do this can be seen below. I have a JavaScript function that received a word to scramble, and then I choose random indexes within a certain limit to create a new scrambled word and return it. If the index I have chosen was already picked, then I try again hoping for a new result.</p>
  
  <pre><code>/**
   * Returns a scrambled word with the first and last letters unchanged
   * that is NOT EQUAL to the given parameter 'word', provided it has 
   * more than three characters.
   */
  function scrambleWord(word){
  
    if(word.length &lt;= 3)
      return word;
  
    var selectedIndexes, randomCharIndex, scrambledWord = word;
  
    while(word === scrambledWord){
      selectedIndexes = [], randomCharIndex, scrambledWord = '';
      scrambledWord += word[0];
      for(var j = 0; j &lt; word.length-2; j++){
  
        //select random char index making sure it is not repeated
        randomCharIndex = getRandomInt(1, word.length-2);
        while(selectedIndexes.indexOf(randomCharIndex) &gt; -1 &amp;&amp; selectedIndexes.length != word.length-2){
          randomCharIndex = getRandomInt(1, word.length-2);
        }
  
        scrambledWord += word[randomCharIndex];
        selectedIndexes.push(randomCharIndex);
      }
      scrambledWord += word[word.length-1];
    }
    return scrambledWord;
  }
  
  /**
   * Returns a random integer between min (inclusive) and max (inclusive)
   * Using Math.round() will give you a non-uniform distribution!
   * See: http://stackoverflow.com/a/1527820/1337392
   */
  function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  </code></pre>
  
  <p>The problem with this approach, is that it is too slow. I fail the tests because I exceed the time limit of 6 seconds, so I definitely need to improve this solution, but I can't see where I can do it.</p>
  
  <p>Any ideas?</p>