Check the divisibility of Hexadecimal numbers | Javascript implementation of the approach ; Function that returns true if s is divisible by m ; Map to map characters to real values ; To store the remainder at any stage ; Find the remainder ; Check the value of remainder ; Driver code
var CHARS = " " ; var DIGITS = 16 ; function isDivisible ( s , m ) { var mp = new Map ( ) ; for ( var i = 0 ; i < DIGITS ; i ++ ) { mp . set ( CHARS [ i ] , i ) ; } var r = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { r = ( r * 16 + mp . get ( s [ i ] ) ) % m ; } if ( ! r ) return true ; return false ; } var s = " " ; var m = 3 ; if ( isDivisible ( s , m ) ) document . write ( " " ) ; else document . write ( " " ) ;