scrimba
Note at 0:00
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 0:00
AboutCommentsNotes
Note at 0:00
Expand for more info
main.js
run
preview
console
function validTime(str) {
if (!str.includes(':')) {
throw 'input must be in HH:MM format'
}

temp = str.split(':');
hour = temp[0];
min = temp[1];

if (hour > 23 || hour < 0) {
return false;
} else if (min > 59 || min < 0) {
return false;
}

return true;
}



/**
* 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);
});

it('throws an error when input is invalid format', () => {
const str = '2310';
const error = 'input must be in HH:MM format';
expect(function() {
validTime(str);
}).toThrow(error);
});
});
Console
"result 1: "
,
false
,
"result 1: "
,
true
,
"result 1: "
,
false
,
/index.html
LIVE