Decrypt a string according to given rules | Function to return the original string after decryption ; Stores the decrypted string ; If length is odd ; Step counter ; Starting and ending index ; Iterate till all characters are decrypted ; Even step ; Odd step ; If length is even ; Step counter ; Starting and ending index ; Even step ; Odd step ; Reverse the decrypted string ; Driver code
function decrypt ( s , l ) { var ans = " " ; if ( l % 2 == 1 ) { var cnt = 0 ; var indl = 0 , indr = l - 1 ; while ( ans . length != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indl ++ ) ; else ans += s . charAt ( indr -- ) ; cnt ++ ; } } else { var cnt = 0 ; var indl = 0 , indr = l - 1 ; while ( ans . length != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indr -- ) ; else ans += s . charAt ( indl ++ ) ; cnt ++ ; } } return ans . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; } var s = " " ; var l = s . length ; document . write ( decrypt ( s , l ) ) ;