Skip to content

Commit 0d560fc

Browse files
committed
refactor formatAs12HourClock function to handle input validation and edge cases
1 parent 481cdd2 commit 0d560fc

1 file changed

Lines changed: 86 additions & 11 deletions

File tree

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

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,98 @@
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
6+
const slicedHours = time.slice(0, 2);
7+
const hours = Number(slicedHours);
8+
const slicedMinutes = time.slice(-2);
9+
const minutes = Number(slicedMinutes);
10+
const pattern = /^\d{2}:\d{2}$/;
11+
if (!pattern.test(time)) {
12+
return `wrong format input`;
13+
}
14+
if (minutes < 60) {
15+
if (hours >= 24) {
16+
return `please check your input hour`;
17+
} else if (hours > 12) {
18+
return `${hours - 12}:${slicedMinutes} pm`;
19+
} else if (hours === 0) {
20+
return `12:${slicedMinutes} am`;
21+
} else {
22+
return `${slicedHours}:${slicedMinutes} am`;
23+
}
24+
} else {
25+
return `please check your input minute`;
926
}
10-
return `${time} am`;
1127
}
1228

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
29+
const currentOutput1 = formatAs12HourClock("00:60");
30+
const targetOutput1 = "please check your input minute";
1531
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
32+
currentOutput1 === targetOutput1,
33+
`current output: ${currentOutput1}, target output: ${targetOutput1}`,
34+
"dude whats wrong",
1835
);
1936

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
37+
const currentOutput2 = formatAs12HourClock("24:45");
38+
const targetOutput2 = "please check your input hour";
2239
console.assert(
2340
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
41+
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
42+
"dude whats wrong",
43+
);
44+
45+
const currentOutput3 = formatAs12HourClock("23:59");
46+
const targetOutput3 = "11:59 pm";
47+
console.assert(
48+
currentOutput3 === targetOutput3,
49+
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
50+
"dude whats wrong",
51+
);
52+
53+
const currentOutput4 = formatAs12HourClock("07:30");
54+
const targetOutput4 = "07:30 am";
55+
console.assert(
56+
currentOutput4 === targetOutput4,
57+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
58+
"dude whats wrong",
59+
);
60+
61+
const currentOutput5 = formatAs12HourClock("22:45");
62+
const targetOutput5 = "10:45 pm";
63+
console.assert(
64+
currentOutput5 === targetOutput5,
65+
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
66+
"dude whats wrong",
2567
);
68+
69+
const currentOutput6 = formatAs12HourClock("00:20");
70+
const targetOutput6 = "12:20 am";
71+
console.assert(
72+
currentOutput6 === targetOutput6,
73+
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
74+
"dude whats wrong",
75+
);
76+
77+
const currentOutput7 = formatAs12HourClock("12:45");
78+
const targetOutput7 = "12:45 pm";
79+
console.assert(
80+
currentOutput7 === targetOutput7,
81+
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
82+
"dude whats wrong",
83+
);
84+
85+
const currentOutput8 = formatAs12HourClock("00:0");
86+
const targetOutput8 = "wrong format input";
87+
console.assert(
88+
currentOutput8 === targetOutput8,
89+
`current output: ${currentOutput8}, target output: ${targetOutput8}`,
90+
"dude whats wrong",
91+
);
92+
93+
console.log(formatAs12HourClock("00:60"));
94+
console.log(formatAs12HourClock("24:45"));
95+
console.log(formatAs12HourClock("23:59"));
96+
console.log(formatAs12HourClock("07:30"));
97+
console.log(formatAs12HourClock("22:45"));
98+
console.log(formatAs12HourClock("00:20"));
99+
console.log(formatAs12HourClock("12:45"));
100+
console.log(formatAs12HourClock("00:0"));

0 commit comments

Comments
 (0)