function validTime(str) {
let isValid = false; //set a boolean to return
//split the string on :, use the first token as hours, wrap in parseInt to have it return as an
integer
const hour = parseInt(str.split(":")[0]);
//split the string on :, use the second token as minutes, wrap in parseInt to have it return as
an integer
const minutes = parseInt(str.split(":")[1]);
//check to see that hours are in hour range and minutes are in minute range.
//This assumes all times will be given in 24hr format. No adjustments are made to account for a
AM/PM distinction
((hour >= 00 && hour)<=23 && (minutes >= 00 && minutes <= 59))? isValid=true : isValid=false;
return isValid;
}
/**
* Test Suite
*/
describe('validTime()', () => {
it('returns true for valid time', () => {
// arrange
const str = '13:58';
// act
const result = validTime(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(true);
});
it('returns false when invalid hours', () => {
// arrange
const str = '25:51';
// act
const result = validTime(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(false);
});
it('returns false when invalid minutes', () => {
// arrange
const str = '02:76';
// act
const result = validTime(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(false);
});
});