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.

1
  • thank you for the response @torazburo, it worked with adding myNewArray[myCounter] = []; before myNewArray[myCounter] = [1]; as explained below by Quince. Commented Nov 4, 2014 at 6:50

1 Answer 1

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);
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the response, worked perfectly. :D added myNewArray[myCounter] = []; and got working
if this issue is solved for then you can mark this as the answer if you want

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.