Pascal's Triangle


Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. To build the triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]
Solution:
/**
 * @param {number} numRows
 * @return {number[][]}
 */
var generate = function(numRows) {
    var result = [];
    
    for (var i=0; i<numRows; i++) {
        if (i === 0) {
            result.push([1]);
            continue;
        }
        var curr = [];
        var desiredSize = i + 1;
        
        for (var j=0; j<desiredSize; j++) {
            if (j === 0) {
              curr.push(1);
            } else if (j === desiredSize - 1) {
              curr.push(1);
            } else {
              curr.push(result[i - 1][j - 1] + result[i - 1][j]);
            }
        }
        result.push(curr);
    }
    return result;
};