/    Sign up×
Community /Pin to ProfileBookmark

looping through string and testing variables

I have a function that produces a string that may look like:

0,22,0,15,16

or it might look like

0,0,0,0,0,0

How can I test for the second condition? i.e. when the function has produced a comma separated list of zeroes. Thanks for any help.

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Aug 04.2004 — What is the function code? How do you plan on testing it? How many numbers will it produce, or is it random?
Copy linkTweet thisAlerts:
@PittimannAug 04.2004 — 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
Copy linkTweet thisAlerts:
@WebskaterauthorAug 04.2004 — Thanks again Pit and thanks steelersfan88 for your response. Pits' answer will do exactly what I want.
Copy linkTweet thisAlerts:
@PittimannAug 04.2004 — Hi!

You're welcome again. ?

Regards - Pit
Copy linkTweet thisAlerts:
@steelersfan88Aug 04.2004 — [i]Originally posted by Pittimann [/i]

[B]Hi!



You're welcome again. ?



Regards [/B]
[/QUOTE]
Copy linkTweet thisAlerts:
@PittimannAug 04.2004 — Hey Kelly! You're a lazybone!!!

Even too lazy to type that yourself. You're stee(a)ling text which I produced by doing that hard typing job.

? :p ? - Pit
Copy linkTweet thisAlerts:
@steelersfan88Aug 04.2004 — hehe, its still early over here ?
Copy linkTweet thisAlerts:
@PittimannAug 04.2004 — Hi!

Then enjoy all the rest of the day. Mine will be over much sooner.

Cheers - Pit
Copy linkTweet thisAlerts:
@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)}

alert ('0,0,0,0'.isZeros())

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@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.

Cheers - Pit
Copy linkTweet thisAlerts:
@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?
Copy linkTweet thisAlerts:
@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$/".
Copy linkTweet thisAlerts:
@steelersfan88Aug 05.2004 — Make a prototype for the boolean:[code=php]<script type="text/javascript">

Boolean.prototype.toString = function() {
return (this == true) ? "yes" : "no";
}

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 ?
Copy linkTweet thisAlerts:
@CharlesAug 05.2004 — Booleans, like all Objects, already have their own "toString()" method. Give the following a try.

[font=monospace]<script type="text/javascript">

<!--

alert(true)

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@steelersfan88Aug 05.2004 — (he wanted yes or no though)
×

Success!

Help @Webskater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...