www.webdeveloper.com
Results 1 to 10 of 10

Thread: randomizing a word

  1. #1
    Join Date
    May 2003
    Posts
    31

    randomizing a word

    Hello,

    I have a form which contains two fields. The first one accepts a word. The second one is the word scrambled. Currently I have to manually scramble the word.

    What I would like to do is after the first field looses focus that it can call a javascript function that automatically scrambles that word.

    i.e.

    apple -> palpe

    Help would be appreciated.

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Post your current code.

    <input type=text onblur="scramble()"> would be an example of an onblur function.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    May 2003
    Posts
    31
    Sorry, I know how to call the function. But I need the code for the function

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    OK, well, do you have any code already? Or are we doing this from scratch?
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Apr 2003
    Location
    South Africa
    Posts
    220

    Not too sure

    Hi -
    I'm not too sure, but this might work...

    function scramble {
    var someText = document.form.inputName.value;
    var index=0;
    var tempString;
    usedIndexes = new Array();
    for (var i=0; i<someText.length(); i++) {
    index=Math.random() * someText.length();
    //check if the index has not already been used - if it has, generate a new one....
    tempString[i] = someText[index];
    }

    //then let the value in the textbox = the generated value...
    document.form.inputName.value = tempString;
    }

    Don't know if that will work - i am but a lowly C/C++ programmer...

    Regards MW
    "Nothing is too true to be wonderful" - Michael Farraday (UCLA)

  6. #6
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    SniperX, I changed your code a little and came up with this. Doesn't work right, but I think it's a start...

    <html><head><script>
    function sink(){
    var someText = document.x.l.value;
    var tempString = new Array(someText.length);
    for(var i=0;i<someText.length;i++)
    index=Math.round(Math.random()*someText.length);
    tempString[i] = someText.charAt(index);
    document.x.l.value = tempString;
    }
    </script></head>
    <body><form name="x">
    <input type=text name="l"><br>
    <input type=button onclick="sink()" value="Scramble">
    </form></body></html>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  7. #7
    Join Date
    Apr 2003
    Location
    South Africa
    Posts
    220

    Hey Jona

    Don't worry about changing my code, its da only way to learn
    "Nothing is too true to be wonderful" - Michael Farraday (UCLA)

  8. #8
    Join Date
    May 2003
    Posts
    31
    Hi guys, thanks for the replies.

    I tested both code and it kind of just hangs and never puts any values in. I will show you what I have for code.

    PHP Code:
     <script language="Javascript">
     <!--
    function 
    scramble() {
       var 
    someText document.addword.word.value
       var 
    index=0
       var 
    tempString
       for(var 
    i=0;i<someText.length;i++) 
          
    index=Math.round(Math.random()*someText.length); 
          
    tempString[i] = someText.charAt(index); 
       } 

       
    //then let the value in the textbox = the generated value... 
       
    document.addword.letters.value tempString;
    }
     
    //-->
     
    </script>

     <form action=admin.php?cmd=addword method=post name=addword>
      <input type=hidden name=form value=sent>

      <table border=0>
       <tr>
          <td><b>Five Letter Word: </b></td>
          <td><input type=text name=word maxlength=5></td>
       </tr>
       <tr>
          <td><b>Five Random Letters From Word: </b></td>
          <td><input type=text name=letters maxlength=5></td>
       </tr>
       <tr>
          <td><input type=submit value="Add Word"></td>
          <td><input type=button onClick="scramble()" value="Scramble"> 
       </tr>
      </table>
     </form> 

  9. #9
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Looks, great. We just have to work on the Javascript part now. Anyhow, I've created my own 'scramble' script that I worked on for a few minutes, but it doesn't work yet...
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  10. #10
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I know this thread is really old, but I figured I'd revive it and provide a solution, since Doobster never replied saying he found one, and I never provided one. So here is working code.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Strict 1.0//EN"
      "http://www.w3.org/TR/xhtml1/dtd/xhtml1-strict.dtd">
    <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>JavaScript Example</title>
     </head>
     <body>
      <h1>JavaScript Example</h1>
      <script type="text/javascript">
         /**********************************************************/
        /*Copyright Jonathan Fenocchi		              */
       /* http://cmm.sonoracabinets.com/jona/index.php           */
      /*  or http://cmm.rgabbard.com/		            */
     /*   Do not use without direct permission from Jonathan   */
    /**********************************************************/
      function get(data){
       queryStr = location.search;
       queryStr = queryStr.split(data+"=")[1];
       queryStr = (queryStr.indexOf("&"))?queryStr.split("&")[0]:queryStr;
      return unescape(queryStr);
     }
    
      var str  = get('str');
     if(str.indexOf(' ') != -1){
      var strs = str.split(" ");
       for(j=0; j<strs.length; j++){
        var str  = strs[j];
        var len  = str.length;
        var ary  = new Array(len);
        var rand = 0;
       for(i=0; i<len; i++){
        rand = Math.floor(Math.random()*str.length);
         if(ary[rand] != rand){
          document.write (str[rand]);
          ary[rand]    = rand;
        } else {
          len++;
        }
      }
          document.write (" ");
     }
    } else {
      var len  = str.length;
      var ary  = new Array(len);
      var rand = 0;
       for(i=0; i<len; i++){
        rand = Math.floor(Math.random()*str.length);
         if(ary[rand] != rand){
          document.write(str[rand]);
          ary[rand] = rand;
        } else {
          len++;
        }
      }
     }
      </script>
     </body>
    </html>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles