-1

I am trying to make a simple JavaScript script where a random word is chosen from an array and then the letters are randomly re-arranged; similar to an anagram. I've tried using .split(); on the word and then using a for loop to randomly show the letters but it doesn't seem to be working. For example:

for (var i = 0; i < splitWord.length; i++) {
    var randomLetter = Math.floor(Math.random() * splitWord.length);
    alert(randomLetter + '<br />');
}

Most, if not all, of the questions that I found appear to be asking how to create a script to solve anagrams rather than actually creating them.

Fiddle: http://jsfiddle.net/TnT2x/

J. Steen
  • 15,470
  • 15
  • 56
  • 63
  • 3
    You haven't really asked a question here, beyond the implicit "write this for me" kind. You're going to have to post your attempted solution's code if you want help with it. – user229044 Jun 28 '14 at 07:59

1 Answers1

0

Try this with the html you've put on JSFiddle :

function shuffle(o){
   for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
   return o;
}
function start() {
   var words = ['question', 'answer', 'guess', 'coach'];
   var splitWord = shuffle(words[Math.floor(Math.random() * words.length)].split(""));
   document.getElementById("box").innerHTML = splitWord.join("");
}
Tanatos
  • 1,857
  • 1
  • 13
  • 12