Printing the Triangle Pattern using last term N | Function to demonstrate printing pattern ; number of spaces ; character to be printed ; outer loop to handle number of rows n in this case ; inner loop to handle number spaces values changing acc . to requirement ; decrementing k after each loop ; inner loop to handle number of columns values changing acc . to outer loop ; printing stars ; ending line after each row ; Function to find the max height or the number of lines in the triangle pattern ; Driver Function
function triangle ( n ) { var k = 2 * n - 2 ; var ch = 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < k ; j ++ ) document . write ( " " ) ; k = k - 1 ; for ( var j = 0 ; j <= i ; j ++ ) { document . write ( ch ++ + " " ) ; } document . write ( " " ) ; } } function maxHeight ( n ) { return parseInt ( ( ( parseInt ( Math . sqrt ( 1 + 8.0 * n ) ) ) - 1 ) / 2 ) ; } var N = 9 ; triangle ( maxHeight ( N ) ) ;