Check whether a binary string can be formed by concatenating given N numbers sequentially | Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1' ; Function that checks whether the binary String can be formed or not ; Empty String for storing the binary number ; check if a [ i ] can be a part of the binary String ; Conversion of var into String ; if a [ i ] can 't be a part  then break the loop ; possible to create binary String ; impossible to create binary String ; Driver code
function isBinary ( n ) { while ( n != 0 ) { var temp = n % 10 ; if ( temp != 0 && temp != 1 ) { return false ; } n = parseInt ( n / 10 ) ; } return true ; } function formBinaryStr ( n , a ) { var flag = true ; var s = " " ; for ( i = 0 ; i < n ; i ++ ) { if ( isBinary ( a [ i ] ) ) s += " " + a [ i ] ; else { flag = false ; break ; } } if ( flag ) document . write ( s + " " ) ; else document . write ( " " ) ; } var a = [ 10 , 1 , 0 , 11 , 10 ] ; var N = a . length ; formBinaryStr ( N , a ) ;