Jump to content

Variable declaration


El_Dudereno

Recommended Posts

I am trying to declare the following variables in a .js file.

 

dow = f or s

cdate = a to p except n

ctime = a to f

city = 0 to 36

 

The problem i have is that i am not sure i have declared the variables correctly, and if i haven't how to correct it.

The code below is my declaration as it is on the page. These variables are called to another page to be used so would there have to be any tags around the code or is it fine without any?

Also if I was to add more functions to this page would they have to go in any tags?

 

My variables:

 

 

 

dow=new Array();

dow[0]=new Array("f");

dow[1]=new Array("s");

 

cdate=new Array();

cdate[0]=new Array("a");

cdate[1]=new Array("b");

cdate[2]=new Array("c");

cdate[3]=new Array("d");

cdate[4]=new Array("e");

cdate[5]=new Array("f");

cdate[6]=new Array("g");

cdate[7]=new Array("h");

cdate[8]=new Array("i");

cdate[9]=new Array("j");

cdate[10]=new Array("k");

cdate[11]=new Array("l");

cdate[12]=new Array("m");

cdate[13]=new Array("o");

cdate[14]=new Array("p");

 

ctime=new Array();

ctime[0]=new Array("a");

ctime[1]=new Array("b");

ctime[2]=new Array("c");

ctime[3]=new Array("d");

ctime[4]=new Array("e");

ctime[5]=new Array("f");

 

city=new Array();

city[0]=new Array("0");

city[1]=new Array("1");

city[2]=new Array("2");

city[3]=new Array("3");

city[4]=new Array("4");

city[5]=new Array("5");

city[6]=new Array("6");

city[7]=new Array("7");

city[8]=new Array("8");

city[9]=new Array("9");

city[10]=new Array("10");

city[11]=new Array("11");

city[12]=new Array("12");

city[13]=new Array("13");

city[14]=new Array("14");

city[15]=new Array("15");

city[16]=new Array("16");

city[17]=new Array("17");

city[18]=new Array("18");

city[19]=new Array("19");

city[20]=new Array("20");

city[21]=new Array("21");

city[22]=new Array("22");

city[23]=new Array("23");

city[24]=new Array("24");

city[25]=new Array("25");

city[26]=new Array("26");

city[27]=new Array("27");

city[28]=new Array("28");

city[29]=new Array("29");

city[30]=new Array("30");

city[31]=new Array("31");

city[32]=new Array("32");

city[33]=new Array("33");

city[34]=new Array("34");

city[35]=new Array("35");

city[36]=new Array("36");

Link to comment
Share on other sites

Your question really doesn't make any sense. Declaring a variable and assigning a value are two different things. Are you saying you want to randomly assign a value to those variables from the arrays? If that is the case then a simple function to randomly return a value from an array is all you need.

 

function getRandFromArray(array)
{
     return array[Math.floor(Math.random()*array.length)];
}

 

Then, to assign a random value from the 'dow' array to another variable just do this

 

var randDOW = getRandFromArray(dow);

 

Also, since your arrays are sequentially based starting at 0, a more efficient way to create them would be like so:

var dow = new Array('f', 's');

var cdate = new Array (
    'a', 'b', 'c', 'd', 'e', 'f', 'g',
    'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p'
    );

var ctime = new Array('a', 'b', 'c', 'd', 'e', 'f');

var city = new Array(
     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '10', '11', '12', '13', '14', '15', '16', '17', '18',
     '19', '20', '21', '22', '23', '24', '25', '26', '27',
     '28', '29', '30', '31', '32', '33', '34', '35', '36'
    );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.