Represent a number N in base | Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Given Input ; Function Call
function BaseConversion ( N ) { let s = " " ; while ( N != 0 ) { if ( N % 2 == 0 ) { s = " " + s ; } else { s = " " + s ; N -- ; } N /= - 2 ; } if ( s == " " ) { s = " " ; } return s ; } let N = - 9 ; document . write ( BaseConversion ( N ) ) ;