0

I am trying to make simple game, where words are loaded from database (check)

put into array (check)

then one after one they are scrambled and displayed with a text field for the user to type in.

In my head it sounds really simple but i have no idea how to scramble the words, in other words how do i work with individual letters of a string.

I am beginner in java script so please gently with me ^^

Tom
  • 421
  • 3
  • 6
  • 12

1 Answers1

0

I'm not familiar with javascript, so I'll just describe an algorithm in pseudocode:

-Make a list/array of all of the letters.

-Randomly choose a number from 1 to n (the number of letters), and take that letter from the list. (note that you'll have to subtract one to get the index).

-Remove that letter from the previous list and add that letter to a new list.

-Rinse and repeat until you have no letters left in the old list.

Sorta related, In python (maybe you can translate it to javascript?), a possible implementation would be

l = list(word)
newWord = ""
for i in xrange(len(word)):
    index = random.range(0, len(l))
    newWord = newWord + l.pop(index)
James
  • 2,635
  • 5
  • 23
  • 30
  • You probably want `newWord += l.pop(index)` or `newWord = newWord + l.pop(index)`; `newWord += newWord + l.pop(index)` likely does not do what you want. Also, you should probably be appending the characters to a list and then joining; that's more efficient because there's less memory allocation. – icktoofay Apr 22 '13 at 01:58
  • Eh yeah, my bad for the +, and I didn't really think there would be much difference for a word (since they are particularly small cases), but you're right. – James Apr 22 '13 at 10:15