Find i 'th Index character in a binary string obtained after n iterations | Javascript Program to find ith character in a binary String . ; Function to store binary Representation ; Function to find ith character ; Function to change decimal to binary ; Assign s1 String in s String ; Driver Code
let s = " " ; function ReverseString ( s ) { let arr = s . split ( " " ) ; for ( let i = 0 ; i < arr . length / 2 ; i ++ ) { let temp = arr [ i ] ; arr [ i ] = arr [ arr . length - i - 1 ] ; arr [ arr . length - i - 1 ] = temp ; } return arr . join ( " " ) ; } function binary_conversion ( m ) { while ( m != 0 ) { let tmp = m % 2 ; s += tmp . toString ( ) ; m = Math . floor ( m / 2 ) ; } s = ReverseString ( s ) ; } function find_character ( n , m , i ) { binary_conversion ( m ) ; let s1 = " " ; for ( let x = 0 ; x < n ; x ++ ) { for ( let y = 0 ; y < s . length ; y ++ ) { if ( s [ y ] == ' ' ) s1 += " " ; else s1 += " " ; } s = s1 ; s1 = " " ; } return s [ i ] - ' ' ; } let m = 5 , n = 2 , i = 8 ; document . write ( find_character ( n , m , i ) ) ;