Skip to content

Commit 28ca897

Browse files
Update format-time.js
testing this formatAs12HourClock function at other times
1 parent 9d651fb commit 28ca897

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3, 5);
8+
if (hours === 0) {
9+
return `12:${minutes} am`;
10+
}
11+
if (hours === 12) {
12+
return `12:${minutes} pm`;
13+
}
714
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
15+
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
916
}
1017
return `${time} am`;
1118
}
1219

1320

14-
const currentOutput = formatAs12HourClock("08:00");
15-
const targetOutput = "08:00 am";
16-
console.assert(
17-
currentOutput === targetOutput,
18-
`Expected ${targetOutput} but got ${currentOutput}`
19-
);
21+
22+
// Tests
23+
console.log(formatAs12HourClock("08:00"));
24+
console.log(formatAs12HourClock("15:45"));
25+
console.log(formatAs12HourClock("12:00"));
26+
console.log(formatAs12HourClock("00:00"));
2027

0 commit comments

Comments
 (0)