Modulus of two Hexadecimal Numbers | Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Given string N and K ; Function Call
function hexaModK ( s , k ) { var mp = new Map ( ) ; for ( var i = 1 ; i <= 9 ; i ++ ) { mp . set ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) , i ) ; } mp . set ( ' ' , 10 ) ; mp . set ( ' ' , 11 ) ; mp . set ( ' ' , 12 ) ; mp . set ( ' ' , 13 ) ; mp . set ( ' ' , 14 ) ; mp . set ( ' ' , 15 ) ; var m = parseInt ( k , 16 ) ; var base = 1 ; var ans = 0 ; for ( var i = s . length - 1 ; i >= 0 ; i -- ) { var n = mp . get ( s [ i ] ) % m ; ans = ( ans + ( base % m * n % m ) % m ) % m ; base = ( base % m * 16 % m ) % m ; } document . write ( ans . toString ( 16 ) . toUpperCase ( ) ) ; } var n = " " ; var k = " " ; hexaModK ( n , k ) ;