Example[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <script language="JavaScript" type="text/javascript"> <!-- var str1='0,22,0,15,16' var str2='0,0,0,0,0,0' check(str1,str2); function check(){ for (var j=0;j<arguments.length;j++){ var count=0; var temp=arguments[j].split(',') for (var i=0;i<temp.length;i++){ if (temp[i]!='0')count++; } if(count==0)alert(arguments[j]+' contains only zeros') else alert('The string "'+arguments[j]+'" contains '+count+' value(s) other than zero') } } //--> </script> </head> <body> </body> </html>[/code]Cheers - Pit
@CharlesAug 04.2004 — #[i]Originally posted by Pittimann [/i]
[B]Hi!
Example[code=php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <script language="JavaScript" type="text/javascript"> <!-- var str1='0,22,0,15,16' var str2='0,0,0,0,0,0' check(str1,str2); function check(){ for (var j=0;j<arguments.length;j++){ var count=0; var temp=arguments[j].split(',') for (var i=0;i<temp.length;i++){ if (temp[i]!='0')count++; } if(count==0)alert(arguments[j]+' contains only zeros') else alert('The string "'+arguments[j]+'" contains '+count+' value(s) other than zero') } } //--> </script> </head> <body> </body> </html>[/code]Cheers - Pit [/B][/QUOTE] Wouldn't it have been easier to just use the following?
[font=monospace]<script type="text/javascript">
<!--
String.prototype.isZeros = function () {return /^(0,)*0$/.test(this)}
@PittimannAug 04.2004 — #Hi!Wouldn't it have been easier to just use the following?[/QUOTE]For you: 100% - for me less % but no problem; for most posters asking for help here, it would work, but not help any further.
You would have to convert all posters to prototypes first.
@WebskaterauthorAug 04.2004 — #I have to admit that although Charles always posts very brief and elegant solutions to questions and I have often used them, I rarely understand them - which is odd because I understand everyone else's posts.
How would I call Charles code (function?)? i.e. How do I pass a string to it so I get a Yes (comma separated string only contains zeroes) or No (is not all zeroes) answer.
I am a poster - what do I have to do change to become a prototype type person?
@CharlesAug 04.2004 — #My "isZeros" method doesn't take any arguments. You don't pass it a string, every string has its own instance of this method.
I'm still typing with one hand, so I'll make this overly brief.
JavaScript has two types of data structures, primitives and objects. The primitives are booleans, numbers and strings. Everything else is an Object. Objects contain methods and properties. The primitives each have wrapper Objects, whenever you use a primitive as if it were an Object it is converted into its wrapper.
Every object has two methods. "object.toString()" is called whenever the object is being treated as a string and "object.valueOf()" is called whenever the object is being treated as a number. Hence given
[font=monospace]<script type="text/javascript">
<!--
alert('123456'.split(''))
// -->
</script>[/font]
The string literal '123456' is converted into a String and then String.split() is called. This returns an Array, but the "alert()" method takes a string. So Array.toString() is called and you get your result, "1,2,3,4,5,6".
There's nothing new here, we all do this stuff all the time. All that assigning a function to "String.prototype.isZeros" does is extend the String Object class to include one more method. You use it just like you use any other String method. It returns a boolean:
[font=monospace]<script type="text/javascript">
<!--
String.prototype.isZeros = function () {return /^(0,)*0$/.test(this)}
alert('123456'.isZeros())
// -->
</script>[/font]
Notice that inside the method we use the key word "this" to refer to the calling object.
But this is all beside the point. I was just wondering why on earth you were trying to do pattern matching without using a regular expression. Especially since its such a simple regular expression in this case, "/^(0,)*0$/".
var bln = true; alert(bln.toString()); bln = false; alert(bln.toString());
</script>[/code]if you want true > yes and false > no.
All of the basics of everything Charles posts is right above. Don't be intimidated, it's quite simple. Maybe mine looks easier because I know where the enter key is ?