PDA

View Full Version : help with for statements



grapevine
05-26-2007, 07:32 AM
I am accessing some information from 4 arrays where I only to want to view certain categories from the array which has 25 variables in it
eg: var trainDestination = new array ('Diss','Norwich','Diss', 'Ipswich',....etc)
eg: var trainTimes = new array('10.00','12.00','13.00','14.00.....etc)

I also created a new array to allocated a number to each of the times
var = trainNumber=new Array(trainTimes.length)

I can write a for statement to view all of the list, but I can not change the statement to view only Diss times or only Norwich times etc. This was my original for statement

var train=0;train<Times.length;train=train+1)
{trainNumber[train]=([train+1])

I have tried changing
var train = 3 to start at the first occurence of say Ipswich but I really do not know how to get it to look at the other occurences.

As you can see I am very novice as this is a new area for me. Any help on how to extract specific bits from the array would be most appreciated.

Many thanks :confused:

jscheuer1
05-26-2007, 07:54 AM
You may want to look into multidimensional arrays:


var schedule=[];
schedule[0]=['Diss','10.00'];
schedule[1]=['Norwich','12.00'];
schedule[2]=['Diss','13.00'];
schedule[3]=['Ipswich','14.00'];
for (var i = 0; i < schedule.length; i++)
if(schedule[i][0]=='Diss')
alert('Train '+[i+1]+' to: '+schedule[i].join(' departs: '));

Or, more instructive perhaps:


var schedule=[];
schedule[0]=['Diss','10.00'];
schedule[1]=['Norwich','12.00'];
schedule[2]=['Diss','13.00'];
schedule[3]=['Ipswich','14.00'];
for (var i = 0; i < schedule.length; i++)
if(schedule[i][0]=='Diss')
alert('Train '+[i+1]+' to: '+schedule[i][0]+' departs: '+schedule[i][1]);