A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Random Character Case

  1. #1
    Junior Member
    Join Date
    Feb 2002
    Location
    Canada
    Posts
    13

    Random Character Case

    Is there any way to take a string in one text box and randomize the case of each character (Upper and Lower), then place it in a new text box?

    Here is my function:

    function calltext() {
    regulartext = reg_text.length;
    for (i=0; i<regulartext; i++) {
    j = Math.ceil(Math.random(2));
    if (j == 1) {
    new_text += reg_text.charAt(i).toUpperCase;
    } else if (j == 2) {
    new_text += reg_text.charAt(i)toLowerCase;
    }
    }
    }

    Thanks for any and all help

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Here is a rewrite of your code that will do it. You were nearly there. A few problems I fixed:

    1) I simplified your random code. random(2) returns either 0 or 1, and works very well for this kind of thing. If it returns 1, it's true, otherwise it's false. You only need a simple if/else construct.

    2. I changed all the variables that are only used inside the function to local vars (using the var) keyword, to help reduce side effects.

    3. I made reg_text a parameter that is passed to the function.

    4. I set a dynamic text field's contents to the results. This code assumes your dynamic text field has been named myTextField.




    code:

    function calltext(reg_text)
    {
    var len = reg_text.length;
    var new_text = '';
    for (var i=0; i < len; i++) {
    if (random(2)) {
    new_text += reg_text.charAt(i).toUpperCase();
    } else {
    new_text += reg_text.charAt(i).toLowerCase();
    }
    }
    trace(new_text); // test output
    return new_text;
    }

    myTextField.text = calltext('Hello how are you');


  3. #3
    Junior Member
    Join Date
    Feb 2002
    Location
    Canada
    Posts
    13
    Thanks, that really helped me a lot.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center