Created
August 9, 2018 16:39
-
-
Save rebz135/efd620f684c3e9b513b91b5f75309522 to your computer and use it in GitHub Desktop.
Pascal's triangle solution
This file contains hidden or 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
| var generate = function(numRows) { | |
| let result = []; | |
| let recursiveFunc = function (roundsLeft = numRows, oldArr = []) { | |
| let newArr = []; | |
| newArr.push(1); | |
| if (!roundsLeft) { | |
| return; | |
| } | |
| for (let i=0; i<oldArr.length; i++) { | |
| if (oldArr[i+1]) { | |
| newArr.push(oldArr[i] + oldArr[i+1]) | |
| } else { | |
| newArr.push(1); | |
| } | |
| } | |
| result.push(newArr); | |
| recursiveFunc(roundsLeft-1, newArr); | |
| } | |
| recursiveFunc(); | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment