Longest Digits Prefix

Description


Given a string, output its longest prefix which contains only digits.

Note: A substring of a string is called a prefix if it starts at the string’s first character.

Example

For inputString="123aa1", the output should be longestDigitsPrefix(inputString) = "123".

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] string inputString

Guaranteed constraints: 3 ≤ inputString.length ≤ 35.

  • [output] string

[JavaScript (ES6)] Syntax Tips

1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
    console.log("This prints to the console when you Run Tests");
    return "Hello, " + name;
}

Solution


1
2
3
4
5
6
7
function longestDigitsPrefix(inputString) {
    for(var i = 0;  i < inputString.length; i++) {
        if(!/\d/.test(inputString[i]))
            break
    }
    return inputString.substring(0,i);
}