Created
February 9, 2021 00:35
-
-
Save Nicknyr/702acd1f647cebcf1cf7ff0594a49c9f to your computer and use it in GitHub Desktop.
CodeSignal: Valid Time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. | |
*/ | |
function validTime(time) { | |
let firstTwo = time.slice(0, 2); | |
let lastTwo = time.slice(3, 5); | |
if(firstTwo < 0 || firstTwo > 23) { | |
return false; | |
} | |
else if(lastTwo < 0 || lastTwo > 59) { | |
return false; | |
} | |
else { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment