Check if decimal representation of Binary String is divisible by 9 or not | Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary Strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary String ; Traverse the binary String ; Stores 3 consecutive characters of the binary String ; Append octal representation of temp ; Function to check if binary String is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the String oct ; Update oddSum ; Traverse the String oct ; Update evenSum ; Stores octal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
function ConvertequivalentBase8 ( S ) { let mp = new Map ( ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; mp . set ( " " , ' ' ) ; let N = S . length ; if ( N % 3 == 2 ) { S = " " + S ; } else if ( N % 3 == 1 ) { S = " " + S ; } N = S . length ; let oct = " " ; for ( let i = 0 ; i < N ; i += 3 ) { let temp = S . substring ( i , i + 3 ) ; oct += mp . get ( temp ) ; } return oct ; } function binString_div_9 ( S , N ) { let oct = " " ; oct = ConvertequivalentBase8 ( S ) ; let oddSum = 0 ; let evenSum = 0 ; let M = oct . length ; for ( let i = 0 ; i < M ; i += 2 ) oddSum += ( oct [ i ] - ' ' ) ; for ( let i = 1 ; i < M ; i += 2 ) { evenSum += ( oct [ i ] - ' ' ) ; } let Oct_9 = 11 ; if ( Math . abs ( oddSum - evenSum ) % Oct_9 == 0 ) { return " " ; } return " " ; } let S = " " ; let N = S . length ; document . write ( binString_div_9 ( S , N ) ) ;