title: Array values are not being read correctly inside gameloop
body: <p>Im creating a program that displays random dots on a canvas, but I am running into problems when I give it rules specifying which dots are supposed to stay lit. I created a canvas with a matching array so for X number of pixels exist an object inside a 2D array. The array is created as follows:</p>
  
  <pre><code>&lt;code&gt;
  //create array
  ctx.grid = []
  for (var i = 0; i &lt; canvasSize; i++) {    
  ctx.grid.push([]);
  for (var e = 0; e &lt; canvasSize; e++) {
  ctx.grid[i].push({light:false, clean:true})
  }
  }
  
  &lt;/code&gt;
  </code></pre>
  
  <p>The gameloop starts creates a random number from 0 to canvasSize*canvasSize
  then maques a divicion to filter this number and get an exact location on my array ctx.grid and sets the light property to true.
  the following loop finds all the objects in the array that have the property light set to true and draws a square on the canvas by calling the drawPoint() function.</p>
  
  <pre><code>&lt;code&gt;
  
  setInterval(gameLoop,10);
  
  function gameLoop() {
      //Get a random number from 0 to canvasSize
      var light = Math.floor(Math.random()*(canvasSize*canvasSize));
      var row = parseInt(light/canvasSize);
      var col = light%canvasSize;
      ctx.grid[row][col].light = true;
  
      //Find which points need to be drawn
      for (var i = 0; i &lt; ctx.grid.length; i++) {
          for (var e = 0; e &lt; ctx.grid[i].length; e++) {
              if (ctx.grid[i][e].light) {
  
                  drawPoint(i,e);
                  findCorner(i,e);
                  clearPoint(i,e);                                                        
  
  
                   }
              }
          }
      }
  
    &lt;/code&gt;
  </code></pre>
  
  <p>I have not included all the program's code because they are not essential to solving this problem.
  Next is the beginning of what is supposed to be a set of rules,specified by findCorner(), that tell the program when some specific points are supposed to remain drawn on the canvas.</p>
  
  <pre><code>&lt;code&gt;
    function findCorner(y,x) {
   //Avoid corners
    if (y != 0 || x != 0 || y != canvasSize || x != canvasSize) { 
  
      if (ctx.grid[y-1][x].light) { //Cannot read property '9' of undefined 
        //another set of rules
         }
      // console.log(ctx.grid);
      // console.log(y);
      // console.log(x);
      // console.log(ctx.grid[y-1][x]);
  
  &lt;/code&gt;
  </code></pre>
  
  <p>When I run the program without the findCorners() function, it runs smoothly meaning my array is set up all right, but it is that if statement that is giving me problems. On the developer tools I get the following error:</p>
  
  <p>Cannot read property '9' of undefined</p>
  
  <p>This appears on the line I commented on my code, and the 'number' is always different.</p>
  
  <p>I added the last console logs on my code because strangely enough when I run my program with those lines, the program runs without any errors though it does run very slowly and makes my browser crash after a while.</p>