Skip to content

Commit e9f2900

Browse files
Refactor formatAs12HourClock function for improved time conversion and add comprehensive test cases
1 parent 8d5e28c commit e9f2900

1 file changed

Lines changed: 77 additions & 5 deletions

File tree

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,95 @@
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

55
function formatAs12HourClock(time) {
6+
// Extract hours as a Number and minutes as a String
67
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
8+
const minutes = time.slice(3, 5);
9+
10+
let formattedHours;
11+
let period;
12+
13+
// Decision Tree for 12-Hour Conversion
14+
if (hours === 0) {
15+
formattedHours = "12";
16+
period = "am";
17+
} else if (hours === 12) {
18+
formattedHours = "12";
19+
period = "pm";
20+
} else if (hours > 12) {
21+
const twelveHour = hours - 12;
22+
formattedHours = twelveHour < 10 ? `0${twelveHour}` : `${twelveHour}`;
23+
period = "pm";
24+
} else {
25+
formattedHours = time.slice(0, 2);
26+
period = "am";
927
}
10-
return `${time} am`;
28+
29+
// Formatting: Reassemble hours, minutes, and period
30+
return `${formattedHours}:${minutes} ${period}`;
1131
}
1232

33+
// Test Cases
34+
1335
const currentOutput = formatAs12HourClock("08:00");
1436
const targetOutput = "08:00 am";
1537
console.assert(
1638
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
39+
`current ${currentOutput}, target ${targetOutput}`
1840
);
1941

2042
const currentOutput2 = formatAs12HourClock("23:00");
2143
const targetOutput2 = "11:00 pm";
2244
console.assert(
2345
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
46+
`current ${currentOutput2}, target ${targetOutput2}`
47+
);
48+
49+
const currentOutput3 = formatAs12HourClock("00:00");
50+
const targetOutput3 = "12:00 am";
51+
console.assert(
52+
currentOutput3 === targetOutput3,
53+
`current ${currentOutput3}, target ${targetOutput3}`
54+
);
55+
56+
const currentOutput4 = formatAs12HourClock("01:00");
57+
const targetOutput4 = "01:00 am";
58+
console.assert(
59+
currentOutput4 === targetOutput4,
60+
`current ${currentOutput4}, target ${targetOutput4}`
2561
);
62+
63+
const currentOutput5 = formatAs12HourClock("12:00");
64+
const targetOutput5 = "12:00 pm";
65+
console.assert(
66+
currentOutput5 === targetOutput5,
67+
`current ${currentOutput5}, target ${targetOutput5}`
68+
);
69+
70+
const currentOutput6 = formatAs12HourClock("13:00");
71+
const targetOutput6 = "01:00 pm";
72+
console.assert(
73+
currentOutput6 === targetOutput6,
74+
`current ${currentOutput6}, target ${targetOutput6}`
75+
);
76+
77+
const currentOutput7 = formatAs12HourClock("15:30");
78+
const targetOutput7 = "03:30 pm";
79+
console.assert(
80+
currentOutput7 === targetOutput7,
81+
`current ${currentOutput7}, target ${targetOutput7}`
82+
);
83+
84+
const currentOutput8 = formatAs12HourClock("23:59");
85+
const targetOutput8 = "11:59 pm";
86+
console.assert(
87+
currentOutput8 === targetOutput8,
88+
`current ${currentOutput8}, target ${targetOutput8}`
89+
);
90+
91+
console.log(formatAs12HourClock("00:00"));
92+
console.log(formatAs12HourClock("01:00"));
93+
console.log(formatAs12HourClock("12:00"));
94+
console.log(formatAs12HourClock("13:00"));
95+
console.log(formatAs12HourClock("15:30"));
96+
console.log(formatAs12HourClock("23:59"));
97+
console.log("All assertions completed!");

0 commit comments

Comments
 (0)