scrimba
Note at 0:08
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:08
AboutCommentsNotes
Note at 0:08
Expand for more info
main.js
run
preview
console
function validTime(str) {
// Split string into array
var timeSplit = str.split(':');

// Convert elements in timeSplit to integers (base 10) and create new array
let timeSplitInt = timeSplit.map(elem=> parseInt(elem, 10));

// Conditions for 24h clock

// First half
if (timeSplitInt[0] < 0 | timeSplitInt[0] > 24) {
return false;
}

// Second half
else if (timeSplitInt[1] < 0 | timeSplitInt[1] > 59) {
return false;
}

else {
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);
});
});
Console
"result 1: "
,
true
,
"result 1: "
,
false
,
"result 1: "
,
false
,
/index.html
LIVE