forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.js
More file actions
Latest commit
28 lines (25 loc) · 578 Bytes
/
Copy pathPascalTriangle.js
File metadata and controls
28 lines (25 loc) · 578 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
constaddRow=(triangle)=>{
constprevious=triangle[triangle.length-1]
constnewRow=[1]
for(leti=0;i<previous.length-1;i++){
constcurrent=previous[i]
constnext=previous[i+1]
newRow.push(current+next)
}
newRow.push(1)
returntriangle.push(newRow)
}
constgenerate=(numRows)=>{
consttriangle=[[1],[1,1]]
if(numRows===0){
return[]
}elseif(numRows===1){
return[[1]]
}else{
for(leti=2;i<numRows;i++){
addRow(triangle)
}
}
returntriangle
}
export{generate}