-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (22 loc) · 974 Bytes
/
index.js
File metadata and controls
25 lines (22 loc) · 974 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
/**
* @param {number} lineNumber - zero based.
* @return {number[]}
*/
const pascalTriangle = (lineNumber) => {
const pascalTrianglePerLine = []; // Its like a multidimensional array (but really not, is just an array of arrays), saves line per line the pascal triangle to return later que specific line
for (let i = 0; i <= lineNumber; i++) {
let newLine = [];
for (let j = 0; j <= i; j++) {
if (j === 0 || j === i) { // The first and last time at the second cicle need have a 1 becaues thats the border of the triangle
newLine.push(1);
} else {
newLine.push(
pascalTrianglePerLine[i - 1][j - 1] + pascalTrianglePerLine[i - 1][j] // If is not the first line, need to take the top left and top right numbers and add them
);
}
}
pascalTrianglePerLine.push(newLine);
}
return pascalTrianglePerLine[lineNumber];
};
module.exports = pascalTriangle;