Created
December 30, 2018 23:44
-
-
Save svngoku/ead331f848d0a324adfee1eba21592ed to your computer and use it in GitHub Desktop.
PascalTriangle created by svngoku - https://repl.it/@svngoku/PascalTriangle
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
| /* Pascal Triangle II */ | |
| const getItem = (row,j) => { | |
| if (j < 0 || j >= row.length) { | |
| return 0; | |
| } | |
| return row[j]; | |
| } | |
| const getRow = (rowIndex) => { | |
| let row = [1]; | |
| for(let i = 0; i < rowIndex; ++i) { | |
| let newRow = []; | |
| for(let j = 0; j <= i; ++j) { | |
| let digit = getItem(row, j-1) + getItem(row, j); | |
| newRow.push(digit); | |
| } | |
| row = newRow; | |
| } | |
| return row; | |
| }; | |
| console.log(getRow(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment