Skip to content

Instantly share code, notes, and snippets.

@noook
Created May 2, 2020 11:30
Show Gist options
  • Select an option

  • Save noook/165b7e6f04d6707f8f229bc5ac42a5a6 to your computer and use it in GitHub Desktop.

Select an option

Save noook/165b7e6f04d6707f8f229bc5ac42a5a6 to your computer and use it in GitHub Desktop.
const tree = [];
for (let i = 1; i < 15; i += 1) {
const row = [];
for (let j = 0; j < i; j += 1) {
if ([0, i - 1].includes(j)) {
row.push(1);
} else {
row.push(tree[tree.length - 1][j - 1] + tree[tree.length - 1][j]);
}
}
tree.push(row);
}
console.table(tree);
/*
┌─────────┬───┬────┬────┬─────┬─────┬──────┬──────┬──────┬──────┬─────┬─────┬────┬────┬────┐
│ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ 12 │ 13 │
├─────────┼───┼────┼────┼─────┼─────┼──────┼──────┼──────┼──────┼─────┼─────┼────┼────┼────┤
│ 0 │ 1 │ │ │ │ │ │ │ │ │ │ │ │ │ │
│ 1 │ 1 │ 1 │ │ │ │ │ │ │ │ │ │ │ │ │
│ 2 │ 1 │ 2 │ 1 │ │ │ │ │ │ │ │ │ │ │ │
│ 3 │ 1 │ 3 │ 3 │ 1 │ │ │ │ │ │ │ │ │ │ │
│ 4 │ 1 │ 4 │ 6 │ 4 │ 1 │ │ │ │ │ │ │ │ │ │
│ 5 │ 1 │ 5 │ 10 │ 10 │ 5 │ 1 │ │ │ │ │ │ │ │ │
│ 6 │ 1 │ 6 │ 15 │ 20 │ 15 │ 6 │ 1 │ │ │ │ │ │ │ │
│ 7 │ 1 │ 7 │ 21 │ 35 │ 35 │ 21 │ 7 │ 1 │ │ │ │ │ │ │
│ 8 │ 1 │ 8 │ 28 │ 56 │ 70 │ 56 │ 28 │ 8 │ 1 │ │ │ │ │ │
│ 9 │ 1 │ 9 │ 36 │ 84 │ 126 │ 126 │ 84 │ 36 │ 9 │ 1 │ │ │ │ │
│ 10 │ 1 │ 10 │ 45 │ 120 │ 210 │ 252 │ 210 │ 120 │ 45 │ 10 │ 1 │ │ │ │
│ 11 │ 1 │ 11 │ 55 │ 165 │ 330 │ 462 │ 462 │ 330 │ 165 │ 55 │ 11 │ 1 │ │ │
│ 12 │ 1 │ 12 │ 66 │ 220 │ 495 │ 792 │ 924 │ 792 │ 495 │ 220 │ 66 │ 12 │ 1 │ │
│ 13 │ 1 │ 13 │ 78 │ 286 │ 715 │ 1287 │ 1716 │ 1716 │ 1287 │ 715 │ 286 │ 78 │ 13 │ 1 │
└─────────┴───┴────┴────┴─────┴─────┴──────┴──────┴──────┴──────┴─────┴─────┴────┴────┴────┘
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment