Skip to content

Instantly share code, notes, and snippets.

@svngoku
Created December 30, 2018 23:44
Show Gist options
  • Select an option

  • Save svngoku/ead331f848d0a324adfee1eba21592ed to your computer and use it in GitHub Desktop.

Select an option

Save svngoku/ead331f848d0a324adfee1eba21592ed to your computer and use it in GitHub Desktop.
PascalTriangle created by svngoku - https://repl.it/@svngoku/PascalTriangle
/* 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