Last active
October 13, 2016 14:02
-
-
Save omnibusinc/c045030f2b1ffb557368e2494765df97 to your computer and use it in GitHub Desktop.
Pascal's Triangle/Pyramid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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