Skip to content

Commit bc87b24

Browse files
committed
Update format-time.js
Code fixed. Tested with various times.
1 parent 5023abf commit bc87b24

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

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

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,56 @@
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.
44

5+
//function formatAs12HourClock(time) {
6+
//const hours = Number(time.slice(0, 2));
7+
//if (hours > 12) {
8+
//return `${hours - 12}:00 pm`;
9+
//}
10+
//return `${time} am`;
11+
//}
12+
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+
//);
26+
27+
// Bug 1: Minutes are hardcoded for PM. It always uses :00 for PM. If the input is "23:45", the output becomes "11:00 pm".
28+
// Bug 2: AM hours are notconverted to 12 hour format. It works for "8:00" but "00:30" becomes "00:30" when it should be "12:30 am".
29+
// Bug 3: 12PM and 12AM are not handled correctly. "12:00" becomes "12:00 am" whjen it should be "12:00 pm", and "00:00" becomes "00:00 am" when it should be "12:00 am".
30+
531
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
32+
let [hours, minutes] = time.split(":");
33+
hours = Number(hours);
34+
let period = "am";
35+
36+
if (hours === 0) {
37+
hours = 12; // midnight
38+
} else if (hours === 12) {
39+
period = "pm"; // noon
40+
} else if (hours > 12) {
41+
hours -= 12;
42+
period = "pm";
943
}
10-
return `${time} am`;
44+
45+
const paddedHours = hours.toString().padStart(2, "0");
46+
47+
return `${paddedHours}:${minutes} ${period}`;
1148
}
1249

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-
);
50+
//testing the function with various inputs.
51+
console.log(formatAs12HourClock("08:00")); // 08:00 am
52+
console.log(formatAs12HourClock("23:00")); // 11:00 pm
53+
console.log(formatAs12HourClock("00:30")); // 12:30 am
54+
console.log(formatAs12HourClock("12:15")); // 12:15 pm
55+
console.log(formatAs12HourClock("13:45")); // 01:45 pm
56+
console.log(formatAs12HourClock("11:59")); // 11:59 am
57+
console.log(formatAs12HourClock("12:00")); // 12:00 pm

0 commit comments

Comments
 (0)