File tree Expand file tree Collapse file tree 1 file changed +26
-28
lines changed Expand file tree Collapse file tree 1 file changed +26
-28
lines changed Original file line number Diff line number Diff line change 1- let numRows = 5
1+ const numRows = 5
22
3- var generate = function ( numRows ) {
4- let 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- } ;
20- var addRow = function ( triangle ) {
21- let previous = triangle [ triangle . length - 1 ]
22- let newRow = [ 1 ]
23- for ( let i = 0 ; i < previous . length - 1 ; i ++ ) {
24- let current = previous [ i ]
25- let next = previous [ i + 1 ]
26- newRow . push ( current + next )
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 )
2715 }
28- newRow . push ( 1 )
29- return triangle . push ( newRow )
30-
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 )
3129}
3230
3331generate ( numRows )
You can’t perform that action at this time.
0 commit comments