Find the missing number in a string of numbers with no separator | Javascript program to find a missing number in a string of consecutive numbers without any separator . ; Gets the integer at position i with length m , returns it or - 1 , if none ; Find value at index i and length m . ; Returns value of missing number ; Try all lengths for first number ; Get value of first number with current length ; To store missing number of current length ; To indicate whether the sequence failed anywhere for current length . ; Find subsequent numbers with previous number as n ; If we haven 't yet found the missing number  for current length. Next number is n+2. Note  that we use Log10 as (n+2) may have more  length than n. ; If next value is ( n + 1 ) ; Not found or no missing number ; Driver code
let MAX_DIGITS = 6 ; function getValue ( str , i , m ) { if ( i + m > str . length ) { return - 1 ; } let value = 0 ; for ( let j = 0 ; j < m ; j ++ ) { let c = str [ i + j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( c < 0 c > 9 ) { return - 1 ; } value = value * 10 + c ; } return value ; } function findMissingNumber ( str ) { for ( let m = 1 ; m <= MAX_DIGITS ; ++ m ) { let n = getValue ( str , 0 , m ) ; if ( n == - 1 ) { break ; } let missingNo = - 1 ; let fail = false ; for ( let i = m ; i != str . length ; i += 1 + Math . floor ( Math . log10 ( n ) ) ) { if ( ( missingNo == - 1 ) && ( getValue ( str , i , Math . floor ( 1 + Math . log10 ( n + 2 ) ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; } else if ( getValue ( str , i , Math . floor ( 1 + Math . log10 ( n + 1 ) ) ) == n + 1 ) { n ++ ; } else { fail = true ; break ; } } if ( ! fail ) { return missingNo ; } } return - 1 ; } document . write ( findMissingNumber ( " " ) ) ;