Program to print Step Pattern | function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Get the String and the number n ; calling the function
function steps ( str , n ) { var flag ; var x = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( x == 0 ) flag = true ; if ( x == n - 1 ) flag = false ; for ( var j = 0 ; j < x ; j ++ ) document . write ( " " ) ; document . write ( str [ i ] + " " ) ; if ( flag == true ) x ++ ; else x -- ; } } var n = 4 ; var str = " " ; document . write ( " " + str + " " ) ; document . write ( " " + n + " " ) ; steps ( str , n ) ;