Longest Digits Prefix
Given a string, output its longest which contains only digits.
Example
For inputString = "123aa1"
, the output should be
longest_digits_prefix(inputString) = "123"
Solution
py
def longest_digits_prefix(input_string):
prefix = ''
for s in input_string:
if s.isdigit():
prefix += s
else:
break
return prefix
print(longest_digits_prefix('123aa1'))
js
function longestDigitsPrefix(inputString) {
let result = '';
for (let i = 0; i < inputString.length; i++) {
if (isNaN(parseInt(inputString[i]))) {
break;
}
result += inputString[i];
}
return result;
}
console.log(longestDigitsPrefix('123aa1'));