Skip to content

Instantly share code, notes, and snippets.

@zacharyhill
Created March 15, 2018 16:43
Show Gist options
  • Save zacharyhill/8135e5840222b0a4a25724e011ccf87b to your computer and use it in GitHub Desktop.
Save zacharyhill/8135e5840222b0a4a25724e011ccf87b to your computer and use it in GitHub Desktop.
var generate = function(numRows) {
var rows = [];
for (var row = 1; row <= numRows; row++) {
rows.push([]);
var rowIndex = row - 1;
var numOfItems = row;
for (var itemNum = 1; itemNum <= numOfItems; itemNum++) {
if (itemNum === 1 || itemNum === numOfItems) {
rows[rowIndex].push(1);
} else {
var lastRow = rowIndex - 1;
var currentNum = rows[lastRow][itemNum - 2] + rows[lastRow][itemNum - 1];
rows[rowIndex].push(currentNum);
}
}
}
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment