JavaScript implementation of Pascal's Triangle I'm currently trying to teach myself JavaScript. If someone could critique my implementation of Pascal's Triangle I would be very grateful.



&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
    &lt;head&gt;
        &lt;meta charset="utf-8"&gt;
        &lt;title&gt;Pascal's Triangle&lt;/title&gt;
        &lt;style&gt;
            .container {
                display: block;
            }

            .row {
                text-align: center;
            }

            .cell {
                border: 1px solid black;
                border-width: 1px 0px 0px 1px;
                display: inline-block;
                width: 40px;
                padding: 10px 0px;
            }

            .row .cell:last-child {
                border-right-width: 1px;
            }

            .container .row:last-child .cell {
                border-bottom-width: 1px;
            }
        &lt;/style&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div id="container" class="container"&gt;&lt;/div&gt;
        &lt;script&gt;
            var rows = 10;
            var pascalsTriangle = [];
            for (var i = 0; i &lt; rows; i++) {
                pascalsTriangle.push([]);
                for (var j = 0; j &lt; rows; j++) {
                    pascalsTriangle[i].push(0);
                }
            }

            pascalsTriangle[0][0] = 1;
            var upperLeft, upper = null;
            for (var y = 1; y &lt; rows; y++) {
                for (var z = 0; z &lt; rows; z++) {
                    upperLeft = pascalsTriangle[y - 1][z - 1] === undefined ? 0 : pascalsTriangle[y - 1][z - 1];
                    upper = pascalsTriangle[y - 1][z];
                    pascalsTriangle[y][z] = upperLeft + upper;
                }
            }

            var html = "";
            for (var a = 0; a &lt; rows; a++) {
                html += '&lt;div class="row"&gt;';
                for (var b = 0; b &lt; rows; b++) {
                    if (pascalsTriangle[a][b] === 0) {
                        continue;
                    }
                    html += '&lt;div class="cell"&gt;' + pascalsTriangle[a][b] + "&lt;/div&gt;";
                }
                html += "&lt;/div&gt;";
            }
            document.getElementById("container").innerHTML += html;
        &lt;/script&gt;
    &lt;/body&gt;
&lt;/html&gt;




My code is also hosted on JSFiddle if anyone wants to see it in action.
I see your code is split into 3 phases: Initializing the triangle, calculating the values and then rendering the HTML. You can actually skip the first phase, and create the triangle on-the-fly, as you calculate.

The last phase we can eliminate one loop (the one that loops through cells) by using some array trickery. Since we're just constructing a string, we can use an array's join method to put markup between array elements. Then we prepend and append the missing markup of the resulting string.

Also, I think you should improve on your variable names. a, b, i, j, y and z don't really mean anything. They're "assumend" to be for iteration purposes. However, someone might see y and z as axes in a graph or something, which may be confusing.

And I think it's easier if you calculate the triangle by assuming that the row will always start with a 1. That way, the second item in the current row (essentially the first one we're actually calculating) is in line with the first item of the previous row. That way, summing is a matter of getting the value of the previous row at the same index, and the one to its right. At the end of the row, the "right" value will be undefined (accessing an out of bounds index) which you can easily replace with a 0.

        1
      1 1
    1 2 1
  1 3 3 1
1 4 9 4 1


With that, here's my take on it:



var rows = 10;

function range(n) {
  return Array.apply(null, Array(n)).map(function(_, i) { return i; });
}

// I use range to create a zero-indexed array of things to iterate.
// It's an alternative to using loops.
var pascalsTriangle = range(rows - 1).reduce(function(carry, index) {
  // Using the previous row, we calculate the next row and add to the tree
  carry.push(carry[index].reduce(function(carry, left, index, previousRow) {
    // Determine the "right"
    carry.push(left + (previousRow[index + 1] || 0));
    return carry;
  // Assume the row always starts with 1
  }, [1]));
  return carry;
// Our initial 1
}, [[1]]);

var html = pascalsTriangle.map(function(row) {
  // Instead of looping through a second time, I use `.join` to fill
  // the markup between. Then prepend and append the missing markup.
  return '&lt;div class="row"&gt;&lt;div class="cell"&gt;' + row.join('&lt;/div&gt;&lt;div class="cell"&gt;') + '&lt;/div&gt;&lt;/div&gt;';
}).join('');

document.getElementById("container").innerHTML += html;
.container {
  display: block;
}
.row {
  text-align: center;
}
.cell {
  border: 1px solid black;
  border-width: 1px 0px 0px 1px;
  display: inline-block;
  width: 40px;
  padding: 10px 0px;
}
.row .cell:last-child {
  border-right-width: 1px;
}
.container .row:last-child .cell {
  border-bottom-width: 1px;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"&gt;&lt;/script&gt;
&lt;div id="container" class="container"&gt;&lt;/div&gt;