DEV Community

loading...
Cover image for Day 12 of JavaScriptmas - Valid Time Solution

Day 12 of JavaScriptmas - Valid Time Solution

Sekti
Keep moving forward. Learn well not learn fast.
・1 min read

Day 12 is checking the value of time.

If the given value is 13:58 will gives true value as a valid time.
If the given value is 25:51 will gives false value as a not valid time.
If the given value is 02:76 will gives false value as a not valid time.

This is the JavaScript solution

function validTime(str) {
    let [hour, min] = str.split(':').map(val => parseInt(val));
    let hoursMark = false, minsMark = false;

    if(hour >= 0 && hour < 24){
        hoursMark = true;
    } 
    if (min >= 0 && min < 60) {
        minsMark = true;
    }

    if(hoursMark && minsMark === true) {
        return true;
    } 
    else {
        return false;
    }

}

Enter fullscreen mode Exit fullscreen mode

Discussion (0)