By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Array is undefined in line of code

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

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. var year = ["1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014","2014"];
  4.  
  5. var plateYear = ["97", "98", "99", "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "131", "132", "141", "142" ];   
  6.  
  7. 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" ]; 
  8.  
  9. 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" ];
  10.  
  11.  
  12. function formGet (form) 
  13. {
  14. event.preventDefault();
  15. console.log(form);
  16. var carYear = form.reg1.value;
  17. console.log("reg1" + carYear);
  18. var b = carYear.toString();
  19. var a = plateYear.indexOf(b);
  20. console.log("a" + a);
  21. var newYear = year [a];
  22. alert("Year Car was made :   "  + newYear);
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. event.preventDefault();
  30. console.log(form);
  31. var carCounties = form.reg2.value;
  32. console.log("reg2" + plates);
  33. var b = counties.toString();
  34. var a = plates.indexOf(b);
  35. console.log("a" + a);
  36. var newCounty = carCounties [a];
  37. alert ("County where car was registered :  " + newCounty);
  38. }
May 7 '14 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
carCounties is a string, not an array.

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

Expand|Select|Wrap|Line Numbers
  1. var counties = {
  2.   C : "Cork",
  3.   CE : "Clare",
  4.   etc.
  5. };
  6. var county = counties[plate];
and the year conversion is easy as well
Expand|Select|Wrap|Line Numbers
  1. var carYear = +form.reg1.value.substr(0,2) + 2000;
  2. if (carYear > 2014) {
  3.   carYear -= 100;
  4. }
May 7 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.