Results 1 to 4 of 4

Thread: Need help with array code

  1. #1
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with array code

    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. #2
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    You may take a look of these code to see if it is help.

    Code:
    function name(num)
    {
    	// store in a array
    	var vdata = new Array(num);
    	for (var vnum = 0; vnum < num; vnum++)
    	{
    		vdata[vnum] = prompt('please enter the name of variable '+vnum+'\: ','');
    
    	}
    Code:
    // new Array('a', 'b', 'c', 'd');
    var a = ["a", "b", "c", "d"]; 
    
    var p = 0;
    var data = '[';
    while (p < a.length)
    {
    	data += "'" + a[p] + "', ";
    	p++;
    }
    data += "'" + a[a.length - 1] + "'];";
    Eric,

  3. #3
    Join Date
    Mar 2009
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi. Thanks for the help This helps However. I did notice 1 small thing. I need it so I can add infinite variables to the same string. example: data += '["'+a+'"]["'+b+'"]'; etc.. +a , +b, +c....

    heres the code:

    Code:
    function name(num)
    {
    	// store in a array
    	var vdata = new Array(num);
    	for (var vnum = 0; vnum < num; vnum++)
    	{
    		vdata[vnum] = prompt('please enter the name of variable '+vnum+'\: ','');
    
    	}
    
    var a = ["a", "b", "c", "d"]; 
    var b = ["e", "f", "g", "h"];
    
    var p = 0;
    var data = '[';
    while (p < a.length-1 || p < b.length-1)
    {
    	data += "'" + a[p] + "', ";
            data += "'" + b[p] + "', ";
    	p++;
    }
    data += "'" + a[a.length - 1] + b[b.length - 1] + "'];";
    
    document.write(data);
    
    }
    Thanks!

    ~ShadowIce~

  4. #4
    Join Date
    May 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I did something wrong, it should be fine now.

    Code:
    <script type="text/javascript">
    // <![CDATA[
    
    String.prototype.trim = function()
    {
    	return this.replace(/^\s*$/g, '');
    }
    
    function name(num)
    {
    	// store in a array
    	var vdata = [];
    	if (typeof(num) != 'number' || isNaN(num)) return;
    
    	for (var vnum = 0; vnum < num; vnum++)
    	{
    		var data = prompt('please enter the name of variable '+vnum+'\: ','');
    		if (data.trim()) vdata.push(data);
    	}
    
    	var data = "['" + vdata.join("', '") + "']";
    }
    // ]]>
    </script>
    Eric,

Bookmarks

Posting Permissions

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