title: How to declare a 3D array and step through it?
body: <p>I was working on this Leetcode problem <a href="https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/description/" rel="nofollow noreferrer">Longest Line of Consecutive One in Matrix</a> <sup>subscription required</sup>, but I cannot figure out why I get an error with regards to indexing. That is, <code>Line 15: TypeError: Cannot set property '0' of undefined</code>.</p>
  
  <p>What's up with this bug? </p>
  
  <pre class="lang-js prettyprint-override"><code>/**
   * @param {number[][]} M
   * @return {number}
   */
  var longestLine = function(M) {
    if(!M.length) return 0;
  
    let ones = 0;
    let dp = [[[]]];
    for(let i=0; i&lt;M.length; i++){
      for(let j=0; j&lt;M[0].length; j++){
        if(M[i][j] === 1){
          //dp[i][j] = undefined ? [] : dp[i][j];
          console.log(dp);
          dp[i][j][0] = i&gt;0 ? dp[i-1][j][0] + 1 : 1;  // ----&gt; line 15                        //left
          dp[i][j][1] = j&gt;0 ? dp[i][j-1][1] + 1 : 1;                          //right
          dp[i][j][2] = (i &gt; 0 &amp;&amp; j &gt; 0) ? dp[i-1][j-1][2]+1 : 1;             //upper left 
          dp[i][j][3] = (i &gt; 0 &amp;&amp; j &lt; M.length - 1) ? dp[i-1][j+1][3] + 1 : 1; //lower left
          ones = Math.max(ones, dp[i][j][0], dp[i][j][1], dp[i][j][2], dp[i][j][3]); 
        }
      }
    }
    return ones;
  };
  </code></pre>