Print Concatenation of Zig | Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
function zigZagConcat ( s , n ) { if ( n <= 1 ) { return s ; } let result = [ ] ; for ( let rowNum = 0 ; rowNum < n ; rowNum ++ ) { let i = rowNum ; let up = true ; while ( i < s . length ) { result . push ( s [ i ] ) ; if ( rowNum == 0 rowNum == n - 1 ) { i += ( 2 * n - 2 ) ; } else { if ( up ) { i += ( 2 * ( n - rowNum ) - 2 ) ; } else { i += rowNum * 2 ; } up ^= true ; } } } return result . join ( " " ) ; } let str = " " ; let n = 3 ; document . write ( zigZagConcat ( str , n ) ) ;