Click to See Complete Forum and Search --> : Word Scrambler


Jona
01-10-2004, 06:21 PM
Hey guys!

I had a question on somethin'. I don't want anyone to code it for me, I just need some thought here... Okay, so I got this script that selects the letters out of a word and then scrambles the letters up, and puts them together to form the scrambled version of the word. This works great, except for two things...

1. Sometimes I end up with the same letters twice.

2. What about words with more than one of the same letter?

I just need kind of an explanation on how I should "validate," if you will, this. I got the code put below all my ranting here, soz if anyone would be so kind as to help me out with the thinking process, I'd be grateful (obviously).


function scramble(t, f){

f.value="";
var ary = new Array(t.value.length);
var temp = "";

for(i=0; i<ary.length; i++)
&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ary[i] = t.value.charAt(i);
&nbsp;}

for(j=0; j<ary.length; j++)
&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rand = Math.round(ary.length*Math.random()) -1;
&nbsp;&nbsp;if(rand<0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{rand++;}
&nbsp;&nbsp;if(rand>4)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{rand--;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp += ary[rand];
&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.value = temp;
}


The form is just two text inputs named t1 and t2, and onchange of t1, the function is run and t2 is updated. In the function t = t1's value and f = t2's value.

Thankies for any help offered! :)

[J]ona

Khalid Ali
01-10-2004, 06:47 PM
Hey Jona,Take a look here I think thats what you want (http://www.webapplikations.com/pages/html_js/document/uniq_random_numbers.html)

Jona
01-10-2004, 06:51 PM
Gee, thanks.

I guess I can "lern frum teh" code. :)

[J]ona