Results 1 to 2 of 2

Thread: help with for statements

  1. #1
    Join Date
    May 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with for statements

    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

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,926
    Thanks
    43
    Thanked 3,191 Times in 3,155 Posts
    Blog Entries
    12

    Default

    You may want to look into multidimensional arrays:

    Code:
    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:

    Code:
    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]);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •