View Single Post
  #6 (permalink)  
Old June 26th, 2008, 06:28 PM
Old Pedant Old Pedant is offline
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

So I couldn't leave well enough alone.

This code works pretty well. Only problem is that every line break becomes two line breaks. Think I know why, but I've finally lost interest <grin/>.

FWIW, I don't think it's true that you can scramble the interior letters of words and still read the text. Probably works for typical newspaper pap, but clearly doesn't work well with technical content. As is now illustrated by the default text.

Code:
<HTML><BODY>
<FORM>
Enter some text:<br/>
<TEXTAREA Name="raw" style="width: 700px; height: 200px;">
String objects can be created implicitly using string literals. 
String objects created in this fashion (referred to as standard strings) 
are treated differently than String objects created using the new operator. 
All string literals share a common, global string object. If a property 
is added to a string literal, it is available to all standard string objects: 
</TEXTAREA>
<P>
<input type=button value="&quot;Translate&quot;" onClick="translate(this.form.raw);">

Translation:<br/>
<TEXTAREA Name="munged" style="width: 700px; height: 200px;" readonly></TEXTAREA>
</FORM>

<script language="javascript">
function mungeWord( word )
{
    var i;
    var letters = new Array();
    for ( i = 0; i < word.length; ++i )
    {
        letters[i] = word.charAt(i);
    }
    var rlen = letters.length - 2;
    for ( i = 1; i < letters.length-1; ++i )
    {
        var rnum = 1 + Math.floor( Math.random() * rlen );
        var temp = letters[i];
        letters[i] = letters[rnum];
        letters[rnum] = temp;
    }
    return letters.join("");
}

function translate( fld )
{
    var txt = fld.value.replace( /([^a-z ])/ig, " @@@$1@@@ " );
    var words = txt.split(/ /);
    for ( var w = 0; w < words.length; ++w )
    {
        var word = words[w];
        if ( word.length > 3 && word.charAt(0) != "@" ) words[w] = mungeWord(word);
    }
    txt = words.join(" ");
    fld.form.munged.value = txt.replace(/@@@([^a-z ])@@@/ig,"$1").replace(/([a-z]) ([^a-z])/ig,"$1$2");
}
</script>
</body></HTML>
Reply With Quote