Consecutive sequenced numbers in a string | function to check consecutive sequential number ; variable to store starting number ; length of the input String ; find the number till half of the String ; new String containing the starting substring of input String ; converting starting substring into number ; backing up the starting number in start ; while loop until the new_String is smaller than input String ; next number ; concatenate the next number ; check if new String becomes equal to input String ; if String doesn 't contains consecutive numbers ; Driver Code
function isConsecutive ( str ) { let start ; let length = str . length ; for ( let i = 0 ; i < length / 2 ; i ++ ) { let new_str = str . substring ( 0 , i + 1 ) ; let num = parseInt ( new_str ) ; start = num ; while ( new_str . length < length ) { num ++ ; new_str = new_str + ( num ) . toString ( ) ; } if ( new_str == ( str ) ) return start ; } return - 1 ; } let str = " " ; document . write ( " " + str + " " ) ; let start = isConsecutive ( str ) ; if ( start != - 1 ) document . write ( " " + start + " " ) ; else document . write ( " " ) ; let str1 = " " ; document . write ( " " + str1 + " " ) ; start = isConsecutive ( str1 ) ; if ( start != - 1 ) document . write ( " " + start + " " ) ; else document . write ( " " ) ;