PDA

View Full Version : Need help with array code



ShadowIce
05-16-2009, 12:10 PM
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:


<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~

Blueray2048
05-16-2009, 12:57 PM
Hi,

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



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+'\: ','');

}




// 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,

ShadowIce
05-16-2009, 01:58 PM
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:


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~

Blueray2048
05-16-2009, 05:04 PM
I did something wrong, it should be fine now.



<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,