Find a number containing N | Function to return the String generated by appending "10" n - 1 times ; Initialising String as empty ; Function to return the decimal equivalent of the given binary String ; Initializing base value to 1 i . e 2 ^ 0 ; Function that calls the constructString and binarytodecimal and returns the answer ; Driver code
function constructString ( n ) { var s = " " ; for ( var i = 0 ; i < n ; i ++ ) { s += " " ; } return s ; } function binaryToDecimal ( n ) { var num = n ; var dec_value = 0 ; var base = 1 ; var len = num . length ; for ( var i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == ' ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } function findNumber ( n ) { var s = constructString ( n - 1 ) ; var num = binaryToDecimal ( s ) ; return num ; } var n = 4 ; document . write ( findNumber ( n ) ) ;