Skip to content

Commit 87642d5

Browse files
Fix 12-hour clock edge cases and add tests
1 parent 70fd1a0 commit 87642d5

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,53 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3);
78

89
if (hours === 0) {
9-
return `12:00 am`;
10+
return `12:${minutes} am`;
1011
}
1112

1213
if (hours === 12) {
13-
return `12:00 pm`;
14+
return `12:${minutes} pm`;
1415
}
1516

1617
if (hours > 12) {
17-
return `${hours - 12}:00 pm`;
18+
return `${hours - 12}:${minutes} pm`;
1819
}
1920

20-
return `${time} am`;
21+
return `${hours}:${minutes} am`;
2122
}
2223

23-
const currentOutput = formatAs12HourClock("08:00");
24-
const targetOutput = "08:00 am";
24+
const currentOutput = formatAs12HourClock("00:34");
25+
const targetOutput = "12:34 am";
2526
console.assert(
2627
currentOutput === targetOutput,
2728
`current output: ${currentOutput}, target output: ${targetOutput}`
2829
);
2930

30-
const currentOutput2 = formatAs12HourClock("23:00");
31-
const targetOutput2 = "11:00 pm";
31+
const currentOutput2 = formatAs12HourClock("12:34");
32+
const targetOutput2 = "12:34 pm";
3233
console.assert(
3334
currentOutput2 === targetOutput2,
3435
`current output: ${currentOutput2}, target output: ${targetOutput2}`
3536
);
3637

3738

38-
const currentOutput3 = formatAs12HourClock("12:00");
39-
const targetOutput3 = "12:00 pm";
39+
const currentOutput3 = formatAs12HourClock("01:01");
40+
const targetOutput3 = "1:01 am";
4041
console.assert(
4142
currentOutput3 === targetOutput3,
4243
`current output: ${currentOutput3}, target output: ${targetOutput3}`
4344
);
4445

45-
const currentOutput4 = formatAs12HourClock("00:00");
46-
const targetOutput4 = "12:00 am";
46+
const currentOutput4 = formatAs12HourClock("13:01");
47+
const targetOutput4 = "1:01 pm";
4748
console.assert(
4849
currentOutput4 === targetOutput4,
4950
`current output: ${currentOutput4}, target output: ${targetOutput4}`
50-
);
51+
);
52+
53+
/*console.log (formatAs12HourClock("00:34"));
54+
console.log (formatAs12HourClock("12:34"));
55+
console.log (formatAs12HourClock("01:01"));
56+
console.log (formatAs12HourClock("13:01"));*/

0 commit comments

Comments
 (0)