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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    For Loop with Condition

    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:

    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!
    Last edited by McMEnterpr; 11-02-2006 at 07:40 AM. Reason: Edited code for clarity

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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:
    Code:
    <!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:
    Code:
    <!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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Or just do this:

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

  • #4
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by McMEnterpr View Post
    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:
    Code:
    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++;
     }

  • #5
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

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

  • #6
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by McMEnterpr View Post
    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.

    Code:
    { 
      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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •