Array is undefined in line of code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arranafc19
    New Member
    • May 2014
    • 1

    Array is undefined in line of code

    In the second function , the array is coming up as undefined. Any Ideas ?

    Code:
    <script type="text/javascript">
    
    var year = ["1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014","2014"];
    
    var plateYear = ["97", "98", "99", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "131", "132", "141", "142" ];   
    
    var counties = ["Cork", "Clare", "Cavan", "Carlow", "Dublin", "Donegal", "Galway", "Kildare", "Kilkenny", "Kerry", "Limerick", "Longford", "Louth", "Leitrim", "Laois", "Meath", "Monaghan", "Mayo", "Offaly", "Roscommon", "Sligo", "North Tipperary", "South Tipperary", "Waterford", "Westmeath", "Wexford", "Wicklow" ]; 
    
    var plates = ["C", "CE", "CN", "CW", "D", "DL", "G", "KE", "KK", "KY", "L", "LD", "LH", "LM", "LS", "MH", "MN", "MO", "OY", "RN", "SO", "TN", "TS", "W", "WH", "WX", "WW" ];
    
    
    function formGet (form) 
    {
    event.preventDefault();
    console.log(form);
    var carYear = form.reg1.value;
    console.log("reg1" + carYear);
    var b = carYear.toString();
    var a = plateYear.indexOf(b);
    console.log("a" + a);
    var newYear = year [a];
    alert("Year Car was made :   "  + newYear);
    
    
    
    
    
    
    event.preventDefault();
    console.log(form);
    var carCounties = form.reg2.value;
    console.log("reg2" + plates);
    var b = counties.toString();
    var a = plates.indexOf(b);
    console.log("a" + a);
    var newCounty = carCounties [a];
    alert ("County where car was registered :  " + newCounty);
    }
    Last edited by Rabbit; May 7 '14, 03:42 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    carCounties is a string, not an array.

    besides that you should rather use objects to avoid data inconsistencies .

    Code:
    var counties = {
      C : "Cork",
      CE : "Clare",
      etc.
    };
    var county = counties[plate];
    and the year conversion is easy as well
    Code:
    var carYear = +form.reg1.value.substr(0,2) + 2000;
    if (carYear > 2014) {
      carYear -= 100;
    }

    Comment

    Working...