Skip to content

Instantly share code, notes, and snippets.

@danieljhegeman
Created January 25, 2018 17:18
Show Gist options
  • Save danieljhegeman/c185905fd2f4124bb8eeae319948f444 to your computer and use it in GitHub Desktop.
Save danieljhegeman/c185905fd2f4124bb8eeae319948f444 to your computer and use it in GitHub Desktop.
Pascal's Triangle - danieljhegeman
var generate = function(numRows) {
result = [];
if (numRows >= 1) {
result = [[1]];
numRows--;
}
for (var i = 0; i < numRows; ++i) {
var prev = result[result.length - 1];
var next = [1]; // initialize
for (var k = 0; k < prev.length - 1; ++k) {
next.push(prev[k] + prev[k + 1]);
}
next.push(1);
result.push(next);
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment