Skip to content

Instantly share code, notes, and snippets.

@omnibusinc
Last active October 13, 2016 14:02
Show Gist options
  • Save omnibusinc/c045030f2b1ffb557368e2494765df97 to your computer and use it in GitHub Desktop.
Save omnibusinc/c045030f2b1ffb557368e2494765df97 to your computer and use it in GitHub Desktop.
Pascal's Triangle/Pyramid
function buildPascalTriangle(maxLevels, currentLevelNum = 0, previousLevel = []) {
var currentLevel = [];
currentLevel.push(1);
previousLevel.forEach(function(item, idx) {
var value = item + previousLevel[idx+1];
currentLevel.push(value);
});
currentLevel[currentLevelNum] = 1;
console.log(currentLevel);
if(currentLevelNum < maxLevels) {
currentLevelNum++;
buildPascalTriangle(maxLevels, currentLevelNum, currentLevel);
}
}
buildPascalTriangle(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment