-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path118-pascalTriangle.ts
More file actions
39 lines (30 loc) · 1020 Bytes
/
Copy path118-pascalTriangle.ts
File metadata and controls
39 lines (30 loc) · 1020 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
33
34
35
36
37
38
39
// Desc.: Given an integer numRows, return the first numRows of Pascal's triangle.
//In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
// [1]
// [1][1]
// [1][2][1]
// [1][3][3][1]
// Expl.: Bellow
const generateTriangle = (numRows: number): number[][] => {
let output = [[1]]
// Creating forloop for "n-times" by numRows and initializing empty array
for (let i = 0; i < numRows-1; i++) {
let array: number[] = []
// Creating forloop for current "n"
for (let j = 0; j <= i+1 ; j++) {
// First and last index is 1
if (j === 0 || j === i+1) {
array.push(1)
// Every other index will be sum of two idx, above this one
} else {
array.push(output[i][j-1] + output[i][j])
}
}
output.push(array)
}
return output
};
console.log(generateTriangle(5))
console.log(generateTriangle(8))
console.log(generateTriangle(3))
console.log(generateTriangle(20))