N | Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code
function binaryToDecimal ( n ) { let num = n ; let dec_value = 0 ; let base = 1 ; let len = num . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == ' ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } function numberSequence ( n ) { if ( n == 1 ) return 1 ; let s = " " ; for ( let i = 1 ; i < n ; i ++ ) s += ' ' ; s += ' ' ; for ( let i = 1 ; i <= n ; i ++ ) s += ' ' ; let num = binaryToDecimal ( s ) ; return num ; } let n = 4 ; document . write ( numberSequence ( n ) ) ;