Skip to content

Commit c7167f3

Browse files
Refactor fixed bugs and tested edge cases
1 parent 8c06db1 commit c7167f3

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
// This is the latest solution to the problem from the prep.
22
// Make sure to do the prep before you do the coursework
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.
4-
4+
55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3, 5);
8+
9+
if (hours === 0) {
10+
return `12:${minutes} am`;
11+
}
12+
13+
if (hours === 12) {
14+
return `12:${minutes} pm`;
15+
}
16+
717
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
18+
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
919
}
20+
1021
return `${time} am`;
1122
}
23+
console.log(formatAs12HourClock("00:00"));
24+
console.log(formatAs12HourClock("01:00"));
25+
console.log(formatAs12HourClock("11:00"));
26+
console.log(formatAs12HourClock("12:00"));
27+
console.log(formatAs12HourClock("13:00"));
28+
console.log(formatAs12HourClock("23:00"));
1229

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
19-
20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22-
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);

0 commit comments

Comments
 (0)