...

View Full Version : For Loop with Condition



McMEnterpr
11-02-2006, 07:37 AM
Hello everyone,

I'm pretty new to javascript and am attempting to create a for loop that outputs text in the following format:

item1, item2, item3, item4

The only problem I'm having is preventing a comma from appearing after the last item.

Currently, the for loop outputs text looking like this:

item1, item2, item3, item4,

I imagine there is some kind of condition I can establish in the for loop to ensure that no comma appears at the end of the list. Does anyone have any suggestions? I'm a bit baffled. Here's my code:


var checkCnt = 0; //count of items
for (cnt=0; cnt < fff.items.length; cnt++) //checking count
if (fff.items[cnt].checked)
{ show.document.write(fff.items[cnt].value+", ");
checkCnt++;
}

if (checkCnt == 0) show.document.write("No selection. <br/>");

Thanks a lot for your help, I really appreciate it!

Kor
11-02-2006, 09:54 AM
There are 2 solutions, in my oppinion. Either you build an array and display it as a string (as in an array the last element has no end comma. Here's an example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">
<script type="text/javascript">
function blah(){
var txt=[];
for(var i=1;i<5;i++){
txt[txt.length]='item'+i
}
document.getElementById('myspan').firstChild.data=txt.toString();
}
onload=blah;
</script>
</head>
<body>
<span id="myspan">&nbsp;</span>
</body>
</html>


Or you use a Regular Expression's method to remove the last comma in a string. Example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="txt/javascript">
<script type="text/javascript">
function blah(){
var txt='';
for(var i=1;i<5;i++){
txt+='item'+i+',';
}
txt=txt.replace(/,$/,'')
document.getElementById('myspan').firstChild.data=txt;
}
onload=blah;
</script>
</head>
<body>
<span id="myspan">&nbsp;</span>
</body>
</html>

Beagle
11-02-2006, 03:04 PM
Or just do this:



var myArray = new Array();
myArray[0] = 'item1';
myArray[1] = 'item2';
myArray[2] = 'item3';
myArray[3] = 'item4';
var theString = myArray.join(',');

Arty Effem
11-02-2006, 03:07 PM
Hello everyone,

I'm pretty new to javascript and am attempting to create a for loop that outputs text in the following format:

item1, item2, item3, item4

The only problem I'm having is preventing a comma from appearing after the last item.
You could add the comma retrospectively:

var checkCnt = 0; //count of items

for(var cnt=0; cnt < fff.items.length; cnt++)
if( fff.items[cnt].checked )
{
show.document.write((checkCnt ? ", " : "")+fff.items[cnt].value);
checkCnt++;
}

McMEnterpr
11-02-2006, 05:53 PM
Thanks for all of your replies!

You've been very helpful. I implemented Arty Effem's suggestion. I have a question, however:

How exactly does this statement prevent their being a comma after the last item? Like I said, I'm new to Javascript, but I would like to understand.



{
show.document.write((checkCnt ? ", " : "")+fff.items[cnt].value);
checkCnt++;
}

Arty Effem
11-02-2006, 06:51 PM
Thanks for all of your replies!

You've been very helpful. I implemented Arty Effem's suggestion. I have a question, however:

How exactly does this statement prevent their being a comma after the last item? Like I said, I'm new to Javascript, but I would like to understand.



{
show.document.write((checkCnt ? ", " : "")+fff.items[cnt].value);
checkCnt++;
}

Because it writes commas prior to the item, provided that there is a previous item, which is more easily checked than the other way around.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum