XOR two binary strings of unequal lengths | Function to insert n 0 s in the beginning of the given string ; Function to return the XOR of the given strings ; Lengths of the given strings ; Make both the strings of equal lengths by inserting 0 s in the beginning ; Updated length ; To store the resultant XOR ; Driver code
function addZeros ( str , n ) { for ( let i = 0 ; i < n ; i ++ ) { str = " " + str ; } return str ; } function getXOR ( a , b ) { let aLen = a . length ; let bLen = b . length ; if ( aLen > bLen ) { a = addZeros ( b , aLen - bLen ) ; } else if ( bLen > aLen ) { a = addZeros ( a , bLen - aLen ) ; } let len = Math . max ( aLen , bLen ) ; let res = " " ; for ( let i = 0 ; i < len ; i ++ ) { if ( a [ i ] == b [ i ] ) res += " " ; else res += " " ; } return res ; } let a = " " , b = " " ; document . write ( getXOR ( a , b ) ) ;