Valid Time

Description


Check if the given string is a correct time representation of the 24-hour clock.

Example

  • For time = "13:58", the output should be validTime(time) = true;
  • For time = "25:51", the output should be validTime(time) = false;
  • For time = "02:76", the output should be validTime(time) = false.

Input/Output

  • [execution time limit] 4 seconds (js)

  • [input] string time

A string representing time in HH:MM format. It is guaranteed that the first two characters, as well as the last two characters, are digits.

  • [output] boolean

true if the given representation is correct, false otherwise.

[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
function validTime(time) {
    return Number(time.split(':')[0]) < 24 && Number(time.split(':')[1]) < 60
}