title: JS: Random String without Repeating Same String Twice
body: <p>I'm trying to figure out a way to pull a random string from an array with pure JS, without the same string appearing twice in succession.</p>
  
  <p>I'm using a random number (<code>random number</code>) to pull a random string from <code>randomMsg</code> using the random number's index, I'm then storing it in <code>currentMsg</code>.</p>
  
  <p>On click, I'm then redefining the <code>currentMsg</code> with a new random string. I'm then using an <code>if</code>/<code>else</code> statement to see if our <code>prevMsg</code> is equal to <code>currentMsg</code>, if it is (which it isn't on first click), I'm using another <code>if</code>/<code>else</code> statement to increment or decrement our index, so it returns a unique string (<code>anotherMsg</code>) to our HTML and sets <code>prevMsg</code> var to that string.</p>
  
  <p>Else, it returns the <code>currentMsg</code> to our HTML, and sets <code>prevMsg</code> equal to <code>currentMsg</code>.</p>
  
  <p>Fucntion below:</p>
  
  <pre><code>(function message() {
      var ranNumber = Math.floor((Math.random() * 5) + 0);
      var randomMsg = [
        "string one",
        "string two",
        "string three",
        "string four",
        "string five"
      ];
  
      var currentMsg = randomMsg[Math.floor((Math.random() * 5) + 0)];
      var prevMsg = "";
  
      document.getElementById("intro").innerHTML = currentMsg;
  
      document.getElementById("intro").onclick = function() {
          var ranNum = Math.floor((Math.random() * 5) + 0);
          currentMsg = randomMsg[ranNum];
          if(prevMsg == currentMsg) {
              var anotherMsg = "";
              if(ranNum &gt;= 0 &amp;&amp; ranNum &lt; 4) {
                  anotherMsg = randomMsg[ranNum + 1];
              }
              else {
                  anotherMsg = randomMsg[ranNum - 1];
              }
              document.getElementById("intro").innerHTML = anotherMsg;
              prevMsg = anotherMsg; // set prevMsg to anotherMsg
          } else {
              document.getElementById("intro").innerHTML = currentMsg;
              prevMsg = currentMsg; // set prevMsg to currentMsg
          };
      };
  })();
  </code></pre>
  
  <p>I've also setup a <a href="http://jsfiddle.net/realph/ygx3vh0q/" rel="nofollow">JSFiddle here</a>.</p>
  
  <p>While it works, I'm eager to now if there's a more elegant approach to write such a function. Also I'm replacing the entire string, is it better to replace just the word (one, two, three, etc.) or doesn't that matter one bit?</p>
  
  <p>Any help is appreciated. Thanks in advance!</p>