Skip to content

Commit d643752

Browse files
committed
Add validation check for whitespace in input string
1 parent ad83dc0 commit d643752

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ function formatAs12HourClock(time = "") {
1414
if (typeof time != "string") {
1515
return "Not a valid format"; //check if time is a string
1616
}
17+
18+
if (/\s/.test(time)) {
19+
return "Not a valid time"; //check for whitespace in string
20+
}
21+
1722
if (
1823
(time.indexOf(":") === -1 && time.indexOf(".") === -1) ||
1924
(time.indexOf(":") !== -1 && time.indexOf(".") !== -1)
@@ -277,3 +282,24 @@ console.assert(
277282
currentOutput29 === targetOutput29,
278283
`current output: ${currentOutput29}, target output: ${targetOutput29}`,
279284
);
285+
286+
const currentOutput30 = formatAs12HourClock(" :30");
287+
const targetOutput30 = "Not a valid time";
288+
console.assert(
289+
currentOutput30 === targetOutput30,
290+
`current output: ${currentOutput30}, target output: ${targetOutput30}`,
291+
);
292+
293+
const currentOutput31 = formatAs12HourClock("12:30 ");
294+
const targetOutput31 = "Not a valid time";
295+
console.assert(
296+
currentOutput31 === targetOutput31,
297+
`current output: ${currentOutput31}, target output: ${targetOutput31}`,
298+
);
299+
300+
const currentOutput32 = formatAs12HourClock("12:30\n");
301+
const targetOutput32 = "Not a valid time";
302+
console.assert(
303+
currentOutput32 === targetOutput32,
304+
`current output: ${currentOutput32}, target output: ${targetOutput32}`,
305+
);

0 commit comments

Comments
 (0)