Find the Nth row in Pascal 's Triangle | Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver code
function getRow ( rowIndex ) { let currow = [ ] ; currow . push ( 1 ) ; if ( rowIndex == 0 ) { return currow ; } let prev = getRow ( rowIndex - 1 ) ; for ( let i = 1 ; i < prev . length ; i ++ ) { let curr = prev [ i - 1 ] + prev [ i ] ; currow . push ( curr ) ; } currow . push ( 1 ) ; return currow ; } let n = 3 ; let arr = getRow ( n ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( i == arr . length - 1 ) document . write ( arr [ i ] ) ; else document . write ( arr [ i ] + " " ) ; }