...

View Full Version : Function too large



luigicannavaro
03-12-2009, 05:22 PM
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

Philip M
03-12-2009, 06:25 PM
Once more into the breach, dear friends!


<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)

mrhoo
03-12-2009, 07:41 PM
I can't resist regular expressions...

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

Philip M
03-12-2009, 07:55 PM
Very neat, mrhoo!

Needs a slight adjustment - if one of the words to be deleted is first in the string it is not deleted.


<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:-


<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.

jmrker
03-12-2009, 08:42 PM
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.

:thumbsup: :D

Philip M
03-12-2009, 09:05 PM
Or, as I often remark,

TIMTOWTOKAC (which is a little more pronounceable)

There is more than one way of killing a cat. :D

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.

jmrker
03-13-2009, 05:46 AM
Or, as I often remark,

TIMTOWTOKAC (which is a little more pronounceable)

There is more than one way of killing a cat. :D

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! :eek: :D

luigicannavaro
03-13-2009, 07:13 AM
Thank you guys.:thumbsup:

Luigi

Philip M
03-13-2009, 08:33 AM
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. :o

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.


<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. :mad:

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


<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. :cool:

luigicannavaro
03-13-2009, 09:06 AM
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

Philip M
03-13-2009, 09:18 AM
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. :(

luigicannavaro
03-13-2009, 09:28 AM
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

Philip M
03-13-2009, 09:46 AM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum