Program to print a string in vertical zigzag manner | Function to print any string in zigzag fashion ; Store the gap between the major columns ; Traverse through rows ; Store the step value for each row ; Iterate in the range [ 1 , N - 1 ] ; Print the character ; Print the spaces before character s [ j + step ] ; Print the character ; Print the spaces after character after s [ j + step ] ; Print the spaces for first and last rows ; Given Input ; Function Call
function zigzag ( s , rows ) { let interval = 2 * rows - 2 ; for ( let i = 0 ; i < rows ; i ++ ) { let step = interval - 2 * i ; for ( let j = i ; j < s . length ; j = j + interval ) { document . write ( s [ j ] ) ; if ( step > 0 && step < interval && step + j < s . length ) { for ( let k = 0 ; k < ( interval - rows - i ) ; k ++ ) document . write ( " " ) ; document . write ( s [ j + step ] ) ; for ( let k = 0 ; k < i - 1 ; k ++ ) document . write ( " " ) ; } else { for ( let k = 0 ; k < ( interval - rows ) ; k ++ ) document . write ( " " ) ; } } document . write ( " " ) ; } } let s = " " + " " + " " ; let rows = 9 ; zigzag ( s , rows ) ;