Skip to content

Commit b74b98b

Browse files
Complete Sprint-3 stretch-extend exercise (format-time.js)
1 parent 28c7638 commit b74b98b

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3);
8+
9+
if (hours === 0) {
10+
return `12:${minutes} am`;
11+
}
12+
if (hours === 12) {
13+
return `12:${minutes} pm`;
14+
}
715
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
16+
return `${hours - 12}:${minutes} pm`;
917
}
1018
return `${time} am`;
1119
}
1220

21+
// Original tests
1322
const currentOutput = formatAs12HourClock("08:00");
1423
const targetOutput = "08:00 am";
1524
console.assert(
@@ -23,3 +32,43 @@ console.assert(
2332
currentOutput2 === targetOutput2,
2433
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2534
);
35+
36+
// Edge case: midnight
37+
const currentOutput3 = formatAs12HourClock("00:00");
38+
const targetOutput3 = "12:00 am";
39+
console.assert(
40+
currentOutput3 === targetOutput3,
41+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
42+
);
43+
44+
// Edge case: noon
45+
const currentOutput4 = formatAs12HourClock("12:00");
46+
const targetOutput4 = "12:00 pm";
47+
console.assert(
48+
currentOutput4 === targetOutput4,
49+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
50+
);
51+
52+
// Edge case: PM time with non-zero minutes (checks minutes aren't lost)
53+
const currentOutput5 = formatAs12HourClock("13:45");
54+
const targetOutput5 = "1:45 pm";
55+
console.assert(
56+
currentOutput5 === targetOutput5,
57+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
58+
);
59+
60+
// Edge case: one minute before midnight
61+
const currentOutput6 = formatAs12HourClock("23:59");
62+
const targetOutput6 = "11:59 pm";
63+
console.assert(
64+
currentOutput6 === targetOutput6,
65+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
66+
);
67+
68+
// Edge case: noon with non-zero minutes
69+
const currentOutput7 = formatAs12HourClock("12:15");
70+
const targetOutput7 = "12:15 pm";
71+
console.assert(
72+
currentOutput7 === targetOutput7,
73+
`current output: ${currentOutput7}, target output: ${targetOutput7}`
74+
);

0 commit comments

Comments
 (0)