We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Learn more about funding links in repositories.
Report abuse
There was an error while loading. Please reload this page.
1 parent 9d4349c commit 67a4f0dCopy full SHA for 67a4f0d
maths/pascalTriangle.js
@@ -0,0 +1,31 @@
1
+const numRows = 5
2
+
3
+var generate = function (numRows) {
4
+ const triangle = [[1], [1, 1]]
5
6
+ if (numRows === 0) {
7
+ return []
8
+ } else if (numRows === 1) {
9
+ return [[1]]
10
+ } else if (numRows === 2) {
11
+ return [[1], [1, 1]]
12
+ } else {
13
+ for (let i = 2; i < numRows; i++) {
14
+ addRow(triangle)
15
+ }
16
17
+ return triangle
18
+}
19
+var addRow = function (triangle) {
20
+ const previous = triangle[triangle.length - 1]
21
+ const newRow = [1]
22
+ for (let i = 0; i < previous.length - 1; i++) {
23
+ const current = previous[i]
24
+ const next = previous[i + 1]
25
+ newRow.push(current + next)
26
27
+ newRow.push(1)
28
+ return triangle.push(newRow)
29
30
31
+generate(numRows)
0 commit comments