-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal-triangle.js
More file actions
32 lines (29 loc) · 793 Bytes
/
Copy pathpascal-triangle.js
File metadata and controls
32 lines (29 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
const printTriangle = t => t.forEach(element => {
let s = '';
element.forEach(e=> s = `${s} ${e}`);
s = s.trim();
console.log(s);
});
const pascalTriangle = (row, max, triangle) => {
if (row === 0) {
return pascalTriangle (row+1, max, [[1]]);
}
if (row === 1) {
return pascalTriangle (row+1, max, triangle.concat([[1,1]]));
}
if (row === max) {
return triangle;
}
let r = triangle[row-1];
let prevElement = 0;
let newRow = [];
r.forEach(e=> {
newRow = newRow.concat(prevElement + e);
prevElement = e;
});
newRow = newRow.concat(1);
return pascalTriangle(row+1, max, triangle.concat([newRow]));
};
var triangle = pascalTriangle(0, 2, [[]]);
printTriangle(triangle);