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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2009
    Posts
    264
    Thanks
    24
    Thanked 1 Time in 1 Post

    Need help on array script

    Hi all I have been doing some research on arrays and how to create multiple, comma seperated strings.

    Now I have a small problem that I cant seem to fix. The problem I have is that I can't get rid of the comma after the last array value has been read. and I can't get the prompt to create more than 1 variable name at once..

    if there's a way to retrieve the comma seperated list along with the double quotes using only 1 document.write, please help. i need the variables to look like this:

    var variable = new Array("comma","seperated","list");

    here is the script i created from various resources:

    Code:
    <script>
    
    function name(num){
    
    var vnum;
    
    for(vnum = 1;vnum<=num;vnum++){
    
    var vname = prompt('please enter the name of variable '+vnum+'\: ','');
    
    }
    
    var a = new Array("a","b","c","d");
    var b = new Array("e","f","g");
    var c = new Array("g","h","i","j");
    
    if(vname != null || vname != ""){
    
    document.write('var '+vname+' = new Array\(');
    
    }
    
    for(var x=0;x<=a.length-1;x++){
    
    if(x.length != a.length-1){
    
    delimiter = ",";
    
    }else{
    
    delimiter = "";
    
    }
    
    document.write('\"'+a[x]+'\"\,');
    
    }
    
    document.write('\)\;');
    
    }
    
    name(4);
    
    </script>
    Thank you!

    ~ShadowIce~

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Most of that makes little, if any, sense. This is not meant to be a criticism, I'm literally telling you it makes no sense. If you can explain why you're trying to create an array like that it would be helpful. I find it odd that you're using document.write to attempt to output an array structure. Seems a bit fishy, maybe the beginnings of some malicious script.

    ...and I can't get the prompt to create more than 1 variable name at once..
    Code:
    for(vnum = 1;vnum<=num;vnum++){
      var vname = prompt('please enter the name of variable '+vnum+'\: ','');
    }
    The problem there is a no-brainer. You create a new instance of the 'vname' variable each iteration through the loop. So when you're done enteriing N values, the last value is the one that actually is assigned and retained. What you want to do is this:
    Code:
    function enterNameList(listLen) {
      var vname= [];  // new array named 'vname'
      for ( var i=0; i<listLen; i+= 1 ) {
        // each name entered is pushed into the array vname
        vname.push( prompt("Enter a name") );
      }
    
      return vname;
    }
    
    var nameList= enterNameList(5);
    
    alert( nameList );
    There. Now you have an array structure already in place, and all the values entered are pushed into it.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,106
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    This will display what you want, but I don't think it will do anything.
    Just displays contents of 'a', 'b', and 'c' arrays delimited as you desire.
    Code:
    <html>
    <head>
    <title>Array Display</title>
    <script type="text/javascript">
    // From: http://www.codingforums.com/showthread.php?t=166473
    
    var vname = new Array();
    function Aname(num) { 
      for(var vnum = 0; vnum<=num; vnum++) {
        vname[vnum] = prompt('please enter the name of variable '+(vnum+1)+'\: ','');
      }
    }
    Aname(2);
    
    var a = new Array("a","b","c","d");
    var b = new Array("e","f","g");
    var c = new Array("g","h","i","j");
    
    var str = '';
    for (var i=0; i<vname.length; i++) {
      str += 'var '+vname[i]+' = new Array("';
      if (i == 0) { str += a.join('","'); }
      if (i == 1) { str += b.join('","'); }
      if (i == 2) { str += c.join('","'); }
      str += '");<br>';
    }
    document.write(str);
    
    </script>
    </head>
    <body>
    Array Display<p>
    </body>
    </html>


  •  

    Posting Permissions

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