Find the n | utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary  palindrome so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
function convertStringToInt ( s ) { let ans = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) ans += 1 << i ; } return ans ; } function getNthNumber ( n ) { let q = [ ] ; if ( n == 1 ) return 1 ; n = n - 1 ; q . push ( " " ) ; while ( n -- > 0 ) { let curr = q . shift ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; let len = curr . length ; let len2 = Math . floor ( len / 2 ) ; if ( len % 2 == 0 ) { q . push ( curr . substring ( 0 , len2 ) + " " + curr . substring ( len2 ) ) ; q . push ( curr . substring ( 0 , len2 ) + " " + curr . substring ( len2 ) ) ; } else { let midChar = curr [ len2 ] ; q . push ( curr . substring ( 0 , len2 ) + midChar + curr . substring ( len2 ) ) ; } } return - 1 ; } let n = 9 ; document . write ( getNthNumber ( n ) ) ;