Encrypt the string | Function to convert Decimal to Hex ; Function to encrypt the string ; Iterate the characters of the string ; Iterate until S [ i ] is equal to ch ; Update count and i ; Decrement i by 1 ; Convert count to hexadecimal representation ; Append the character ; Append the characters frequency in hexadecimal representation ; Reverse the obtained answer ; Return required answer ; Given Input ; Function Call
function convertToHex ( num ) { let temp = " " ; while ( num != 0 ) { let rem = num % 16 ; let c = 0 ; if ( rem < 10 ) { c = rem + 48 ; } else { c = rem + 87 ; } temp += String . fromCharCode ( c ) ; num = Math . floor ( num / 16 ) ; } return temp ; } function encryptString ( S , N ) { let ans = " " ; for ( let i = 0 ; i < N ; i ++ ) { let ch = S [ i ] ; let count = 0 ; let hex ; while ( i < N && S [ i ] == ch ) { count ++ ; i ++ ; } i -- ; hex = convertToHex ( count ) ; ans += ch ; ans += hex ; } ans = ans . split ( ' ' ) . reverse ( ) . join ( " " ) ; return ans ; } let S = " " ; let N = S . length ; document . write ( encryptString ( S , N ) ) ;