title: Javascript 2D array function
body: <p>I have a 2D array, which contains either <code>true</code> or <code>false</code>.
  I made a function that returns the number of neighbors (in all 8 directions) in that array, which are <code>true</code>.
  But for some unknown reason, it does not work corectly (returns wrong number of neighbors).
  (Yes, I'm making Conway's Game of Life.)</p>
  
  <pre><code>function neighbors(seq, x, y) {
     var cnt = 0;
     try {
      if (seq[y-1][x]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y][x-1]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y][x+1]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y+1][x]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y-1][x+1]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y-1][x-1]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y+1][x-1]){
          cnt++;
      }
     }
     catch(err) {
  
     }
     try {
      if (seq[y+1][x+1]){
          cnt++;
      }
     }
     catch(err) {
  
     }        
     return cnt;
  }
  </code></pre>
  
  <p>This code was basically translated from my Python code, that works.</p>