Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Script to reverse lines won't work

    This is absolutely the darnedest thing I have ever seen, and when I think I've seen it all, this takes the cake. OK, I copied an example out of a Beginning JavaScript book - and I mean word for word. The script is supposed to reverse lines in a text area. I am posting two versions of this script: the one I copied out of the book, and a downloaded copy of the same script. Here's the kicker: the downloaded copy works absolutely beautifully, but mine doesn't work at all - and the code is IDENTICAL! Oh, and btw, the browser found no errors in my code. I am posting both of them, and you tell me if there is anything different. Here's mine:

    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE=JavaScript>
    function splitAndReverseText(textAreaControl)
    {
      var textToSplit = textAreaControl.value;
      var textArray = textToSplit.split('\r\n');
    
      var numberOfParts = 0;
      numberOfParts = textArray.length;
    
      var reversedString = "";
      var indexCount;
    
      for (indexCount = numberOfParts - 1; indexCount >= 0; indexCount--)
      {
        reversedString = reversedString + textArray[indexCount];
        if (indexCount > 0)
        {
          reversedString = reversedString + "\r\n";
        }
      }
      textAreaControl.value = reversedString;
    }
    </SCRIPT>
    </HEAD>
    
    <BODY>
    <FORM NAME=form1>
    <TEXTAREA ROWS=20 COLS=40 NAME=textarea1 WRAP=soft>Line 1
      Line 2
      Line 3
      Line 4</TEXTAREA>
    <BR>
    <INPUT TYPE="button" VALUE="Reverse Line Order" NAME=buttonSplit onclick="splitAndReverseText(document.form1.textarea1)">
    </FORM>
    </BODY>
    </HTML>
    Now the downloaded copy:

    Code:
    <HTML>
    <HEAD>
    <SCRIPT LANGUAGE=JavaScript>
    function splitAndReverseText(textAreaControl)
    {
       var textToSplit = textAreaControl.value;
       var textArray = textToSplit.split('\r\n');
    
       var numberOfParts = 0;
       numberOfParts = textArray.length;
    
       var reversedString = "";
       var indexCount;
    
       for (indexCount = numberOfParts - 1; indexCount >= 0; indexCount--)
       {
          reversedString = reversedString + textArray[indexCount];
          if (indexCount > 0)
          {
             reversedString = reversedString + "\r\n";
          }
       }
    
       textAreaControl.value = reversedString;
    }
    </SCRIPT>
    </HEAD>
    
    <BODY>
    <FORM NAME=form1>
    <TEXTAREA ROWS=20 COLS=40 NAME=textarea1 WRAP=soft>Line 1
    Line 2
    Line 3
    Line 4</TEXTAREA>
    <BR>
    <INPUT TYPE="button" VALUE="Reverse Line Order" NAME=buttonSplit
       onclick="splitAndReverseText(document.form1.textarea1)">
    </FORM>
    </BODY>
    </HTML>

  • #2
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts
    It may be the \r\n at lines 7 and 20.

    \r\n is a Windows end of line.

    \n is a Unix/Mac OSX end of line.

    \r (I think) is a Mac OS 9 and earlier end of line.

    When the line ending of the content in the textarea field is different than what the JavaScript is looking for, then the lines won't be split. Without them being split, they can't be reordered.

    Try saving both versions with the same line endings. Or, alternatively, replace \r\n with \n on the two lines and see if it will work.

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    You could try textToSplit.split( /\r?\n/ );

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,133
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    Quote Originally Posted by GreenhornPup View Post
    That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?
    I copied and pasted both of your posts and neither one worked.

    Altered to .split(/\r?\n/) and all works well.

  • #6
    Regular Coder
    Join Date
    Jun 2012
    Location
    Near Chicago, USA
    Posts
    123
    Thanks
    7
    Thanked 19 Times in 19 Posts
    Quote Originally Posted by GreenhornPup View Post
    That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?
    Actually, they're not identical. One has 845 characters and the other 864. When two files have identical visible characters, yet behave differently, the invisible characters (spaces, tabs, line endings, etc.) may be the reason for the different behavior.

    Will
    Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]


  •  

    Tags for this Thread

    Posting Permissions

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