1

I have searched alot and cannot get a clear answer to my problem.

var rowCount = 3;
var myCounter = 0;
var myNewArray = new Array();

for (var i = 1; i < rowCount; i++) {
    try {
        myNewArray[myCounter][0] = i;                                   
        myNewArray[myCounter][1] = i;

        myCounter = myCounter + 1;

    } catch (err) {
        alert(err.message);
    }
}

it is giving and exception saying myNewArray[myCounter] is undefined. Any idea why? I have seen other post and all have shown to declare the array like this or with new Array([]). Nothing is working. Need help, in advance thank you.

Alz
  • 351
  • 5
  • 17
  • thank you for the response @torazburo, it worked with adding myNewArray[myCounter] = []; before myNewArray[myCounter] = [1]; as explained below by Quince. – Alz Nov 04 '14 at 06:50

1 Answers1

2

Currently myNewArray is an array but the elements you are trying to access in it do not yet exist (undefined) so you would need to set these elements as arrays

 var rowCount = 3;
 var myCounter = 0;
 var myNewArray = new Array();

 for (var i = 1; i < rowCount; i++) {
     try {
         //set this element as an array if you want to then access it as an array
         myNewArray[myCounter] = [];
         myNewArray[myCounter][0] = i;
         myNewArray[myCounter][1] = i;

         myCounter = myCounter + 1;

     } catch (err) {
         alert(err.message);
     }
 }
Quince
  • 14,790
  • 6
  • 60
  • 69
  • thank you for the response, worked perfectly. :D added myNewArray[myCounter] = []; and got working – Alz Nov 04 '14 at 06:49
  • if this issue is solved for then you can mark this as the answer if you want – Quince Nov 04 '14 at 08:29