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 13 of 13
  1. #1
    Regular Coder luigicannavaro's Avatar
    Join Date
    Aug 2007
    Posts
    150
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Function too large

    Hi,

    This function is too large. I think there is a way to write the same information with less code or not? ould you please help me out?


    <script>
    function cleanIt(){
    var string=document.frmName.Input.value;
    string=string.replace(/ the /g," ");
    string=string.replace(/ in /g," ");
    string=string.replace(/ of /g," ");
    string=string.replace(/ on /g," ");
    string=string.replace(/ and /g," ");
    string=string.replace(/ A /g," ");
    string=string.replace(/ a /g," ");

    string=string.replace(/ to /g," ");
    string=string.replace(/ for /g," ");
    string=string.replace(/ is /g," ");
    string=string.replace(/ at /g," ");
    string=string.replace(/ As /g," ");
    string=string.replace(/ she /g," ");
    string=string.replace(/ he /g," ");
    string=string.replace(/ it /g," ");
    string=string.replace(/ An /g," ");

    string=string.replace(/1/g," ");
    string=string.replace(/2/g," ");
    string=string.replace(/3/g," ");
    string=string.replace(/4/g," ");
    string=string.replace(/5/g," ");
    string=string.replace(/6/g," ");
    string=string.replace(/7/g," ");
    string=string.replace(/8/g," ");
    string=string.replace(/9/g," ");
    string=string.replace(/0/g," ");

    document.frmName.Input.value=string;
    }
    </script>
    Best regards

    Luigi

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Once more into the breach, dear friends!

    Code:
    <script type = "text/javascript">
    
    function cleanIt() {
    //var string = document.frmName.Input.value;
    var string = "in the quick on 3 bro55wn fox jumps over 7 in the lazy she dog";  // example
    var string = string.replace(/[0-9]/g, "");  // strip numbers
    var stringArray = string.split(" ");
    var strlen = stringArray.length;
    
    var words = new Array("the","in","of","on","she","An");  // add other words as desired
    var wdlen = words.length;
    
    for (var i =0; i < strlen; i++) {
    for (var j =0; j < wdlen; j ++) {
    if (stringArray[i] == words[j]) {
    stringArray[i] = "";
    }
    }
    }
    
    var newString = stringArray.join(" ");
    newString = newString.replace(/\s+/g," ");
    
    alert (newString);
    
    }
    
    cleanIt();
    
    </script>

    “Going to church does not make you a Christian anymore than going to the garage makes you a car.” - Dr. Laurence J. Peter (American "hierarchiologist", Educator and Writer, 1919-1990)
    Last edited by Philip M; 03-12-2009 at 06:32 PM.

  • Users who have thanked Philip M for this post:

    luigicannavaro (03-13-2009)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    I can't resist regular expressions...
    Code:
    function cleanstring(){
    	var str=document.frmName.Input.value;
    	var reps= 'the|in|of|on|A|a'; //add all you like, delimited with '|'
    	var Rx= RegExp('\\d+|( +('+reps+') +)','g');
    	document.frmName.Input.value=str.replace(Rx,'')
    }
    If you want to save spaces for the replaced items, as in your function:
    =str.replace(Rx,' ').replace(/ +/g,' ').replace(/^ | $/g,'');

    the second replace is to condense multiple sequential spaces to single spaces
    the third cleans any spaces from the start and end, if you start or end with a digit
    Last edited by mrhoo; 03-12-2009 at 07:51 PM.

  • The Following 2 Users Say Thank You to mrhoo For This Useful Post:

    luigicannavaro (03-13-2009), Philip M (03-12-2009)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Very neat, mrhoo!

    Needs a slight adjustment - if one of the words to be deleted is first in the string it is not deleted.
    Code:
    <script type = "text/javascript">
    function cleanstring(){
    var str = "in the quick on 3 bro55wn fox jumps over 7 in the lazy she dog";  // example
    var reps= 'the|in|of|on|A|a'; //add all you like, delimited with '|'
    var Rx= RegExp('\\d+|( +('+reps+') +)','g');
    str1 = str.replace(Rx,'');
    alert (str1);  // inquick brown fox jumps over the lazy she dog 
    }
    
    cleanstring()
    </script>
    This can be overcome with the following alteration:

    var Rx= RegExp('\\d+|(\s*('+reps+') +)','g');

    Also, the line
    str1 = str.replace(Rx,' ').replace(/ +/g,' ').replace(/^ | $/g,'');
    does not remove the space in br own (the numbers 55 having been deleted).

    So my revison of your code is:-

    Code:
    <script type = "text/javascript">
    function cleanstring(){
    var str = "in the quick on 3 bro55wn fox jumps over 7 in the lazy she dog";  // example
    var reps= 'the|in|of|on|A|a'; //add all you like, delimited with '|'
    var Rx= RegExp('\\d+|(\s*('+reps+') +)','g');
    str1 = str.replace(Rx,'');
    str1 = str1.replace(/\s+/g," ");
    
    alert (str1);  // quick brown fox jumps over lazy she dog 
    
    }
    
    cleanstring()
    </script>
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.
    Last edited by Philip M; 03-12-2009 at 08:40 PM.

  • The Following 2 Users Say Thank You to Philip M For This Useful Post:

    luigicannavaro (03-13-2009), mrhoo (03-12-2009)

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

    Thumbs up

    Wow ... shades of PERL.
    For learning Perl the TMTOWTDI paradigm of Perl applies:
    there's more than one way to do it.
    Now we can add:
    For learning Javascript the TMTOWTDI paradigm of Javascript applies:
    there's more than one way to do it.


  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Or, as I often remark,

    TIMTOWTOKAC (which is a little more pronounceable)

    There is more than one way of killing a cat.

    I have to say that I tend to the pragmatic - if it works then it is usually OK.
    And if it ain't broke, don't fix it.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Smile

    Quote Originally Posted by Philip M View Post
    Or, as I often remark,

    TIMTOWTOKAC (which is a little more pronounceable)

    There is more than one way of killing a cat.

    I have to say that I tend to the pragmatic - if it works then it is usually OK.
    And if it ain't broke, don't fix it.
    Don't let the ASPCA or PETA people know of your where abouts!

  • #8
    Regular Coder luigicannavaro's Avatar
    Join Date
    Aug 2007
    Posts
    150
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thank you guys.

    Luigi

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Final Version (I hope)!

    In the middle of the night I realised that there is a flaw in my modification of mrhoo's code, in that it will delete a word which is a character sequence within another word, e.g. it would remove the in in string.

    So it is nececssary to add a space at the start of the string to catch the first word and revert to the original regex.

    Code:
    <script type = "text/javascript">
    function cleanstring(){
    var str = "in the quick on 3 bro55wn string a fox jumps over 7 in the lazy she dog";  // example
    str = " " + str;
    var reps= 'the|in|of|on|A|a'; //add all you like, delimited with '|'
    var Rx= RegExp('\\d+|( +('+reps+') +)','g');
    str1 = str.replace(Rx,'');  // remove unwanted whole words
    str1 = str1.replace(/\s+/g," ");  // remove excess spaces
    
    alert (str1);
    }
    
    cleanstring()
    </script>
    But there is another defect. In the above the single a is removed but the two words either side are concatenated to stringfox.

    If I change
    str1 = str.replace(Rx,'');
    to
    str1 = str.replace(Rx,' ');

    to deal with this, and the 55 is removed leaving br own.

    So my final solution is to start with stripping numbers, and add a space at the start of the string.

    Code:
    <script type = "text/javascript">
    function cleanstring() {
    var str = "in the quick on 3 bro55wn fox jumps over 7 in the lazy she dog";  // example
    str = " " + str;
    str = str.replace(/[0-9]/g,"");
    var reps= 'the|in|of|on|A|a'; //add all you like, delimited with '|'
    var Rx= RegExp('\\d+|(\s*('+reps+') +)','g');
    str1 = str.replace(Rx,'');
    str1 = str1.replace(/\s+/g," ");
    
    alert (str1);  // quick brown fox jumps over lazy she dog 
    
    document.write(str1);
    }
    
    cleanstring()
    </script>

    Although I also have a fondness for regex, this is an example of why exhaustive testing may be needed if strange and unexpected results are to be avoided. For myself I think I will stick with my plain vanilla array and two "for" loops method.
    Last edited by Philip M; 03-13-2009 at 08:45 AM. Reason: Typo

  • #10
    Regular Coder luigicannavaro's Avatar
    Join Date
    Aug 2007
    Posts
    150
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Dear Philip,

    Unfortunately your code does not "strip out" the "stop words" (a,an,are,the,or,in,on).

    No problems. I have you in great consideration!

    Best regards

    Luigi

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by luigicannavaro View Post
    Dear Philip,

    Unfortunately your code does not "strip out" the "stop words" (a,an,are,the,or,in,on).

    No problems. I have you in great consideration!

    Best regards

    Luigi

    Luigi,

    As so often, the code I posted works fine for me but you seem to have trouble with it. I never post code which I have not tested, and it is somewhat irritating to be told that it does not work when I have put in quite a lot of effort in meeting your needs.

  • #12
    Regular Coder luigicannavaro's Avatar
    Join Date
    Aug 2007
    Posts
    150
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Yes. Your code works fine for that example:

    var str = "in the quick on 3 bro55wn string a fox jumps over 7 in the lazy she dog";
    And certainly any other text in its place. I was testing it in my textarea and I have "rejected" your example.

    Ok.

    Luigi

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by luigicannavaro View Post
    Yes. Your code works fine for that example:



    And certainly any other text in its place. I was testing it in my textarea and I have "rejected" your example.

    Ok.

    Luigi
    I haven't a clue what you are talking about. And this is the first time you have mentioned a textarea.

    var str = documentGetElementById("textAreaName").value;


    By the way, I wonder if I might ask whether you are a professional web developer, in other words is someone paying you to produce the code which you are regularly getting from this forum for nothing?
    Last edited by Philip M; 03-13-2009 at 09:50 AM.


  •  

    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
    •