validTime
with Carmen Salas • 2024/10/29
Code Challenge
/*
## PEDAC
**Problem**
input: string representing time in the format: HH:MM
output: boolean representing if the string representation is correct or otherwise
task: validate the time string
**Examples/Edge Case:**
22:61 -> false
23:23 -> true
**Data Structure**
- Strings
- Arrays
- Numbers
- Conditional Statements to validate
- (maybe) regex
**Algorithm**
WOW THESE MAY BE SKIPPABLE TOO LMAO READING
1. Find a colon in the string (if not present, INVALIDATE)
2. Check if length of string > 5 (if yes, INVALIDATE)
3. Split string into array based on ":" as separator.
ME WHEN I DONT READ STEP 4 IS SKIPPABLE LOL
4. Convert both strings into Numbers. (if either side can't be converted, INVALIDATE)
4.a Check if left side is between 00-23 (otherwise, INVALIDATE)
4.b Check if right side is between 00-59 (otherwise, INVALIDATE)
5. Return true (since all cases should already be covered)
NEW ALGORITHM BECAUSE I DONT READ
1. Split string into array based on ":" as separator
2. Check if left side is between 00-23 (otherwise, INVALIDATE)
3. Check if right side is between 00-59 (otherwise, INVALIDATE)
4. Return true (since all cases should already be covered)
**Code**
*/
function solution(time) {
// // WOW THESE MAY BE SKIPPABLE TOO LMAO READING
// if (!time.includes(":")) { return false; } // step #1
// if (time.length > 5) { return false; } // step #2
// const timeArr = time.split(":") // step #3
// // for clarity we can name the values
// const hours = Number(timeArr[0]);
// const minutes = Number(timeArr[1]);
// // in hindsight, will this return an error anyway?
// // how do u stop it if input is actually NaN?
// // NVM it works!
// // step #4
// // ME WHEN I DONT READ STEP 4 IS SKIPPABLE LOL
// if (isNaN(hours)) { return false; }
// if (isNaN(minutes)) { return false; }
// if(hours < 0 || hours > 23) { return false; } // step #4a
// if(minutes < 0 || minutes > 59) { return false; } // step #4b
// return true; // step #5
// NEW ALGO WITH READING COMPREHENSION INTO ACCOUNT
// Step #1
const timeArr = time.split(":");
// Name the sides for ease of use
const hours = Number(timeArr[0]);
const minutes = Number(timeArr[1]);
// Step #2
if (hours < 0 || hours > 23) { return false; }
// Step #3
if (minutes < 0 || minutes > 59) { return false; }
// Step #4
return true;
}
Last updated