Skip to content

Commit b970084

Browse files
committed
wrote more test cases - found the bug and updated the code
1 parent 5ee1594 commit b970084

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,58 @@
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+
function pad(num) {
5+
let numString = num.toString();
6+
while (numString.length < 2) {
7+
numString = "0" + numString;
8+
}
9+
return numString;
10+
}
411

512
function formatAs12HourClock(time) {
613
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
14+
const minutes = time.slice(3, 5);
15+
if (hours === 0) {
16+
return `12:${minutes} am`;
17+
} else if (hours > 12) {
18+
return `${pad(hours - 12)}:${minutes} pm`;
19+
} else if (hours === 12) {
20+
return `12:${minutes} pm`;
921
}
10-
return `${time} am`;
22+
return `${pad(hours)}:${minutes} am`;
1123
}
1224

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
25+
const currentOutput = formatAs12HourClock("06:00");
26+
const targetOutput = "06:00 am";
1527
console.assert(
1628
currentOutput === targetOutput,
1729
`current output: ${currentOutput}, target output: ${targetOutput}`
1830
);
1931

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
32+
const currentOutput2 = formatAs12HourClock("12:00");
33+
const targetOutput2 = "12:00 pm";
2234
console.assert(
2335
currentOutput2 === targetOutput2,
2436
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2537
);
38+
39+
const currentOutput3 = formatAs12HourClock("00:00");
40+
const targetOutput3 = "12:00 am";
41+
console.assert(
42+
currentOutput3 === targetOutput3,
43+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
44+
);
45+
46+
const currentOutput4 = formatAs12HourClock("22:59");
47+
const targetOutput4 = "10:59 pm";
48+
console.assert(
49+
currentOutput4 === targetOutput4,
50+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
51+
);
52+
53+
const currentOutput5 = formatAs12HourClock("21:30");
54+
const targetOutput5 = "09:30 pm";
55+
console.assert(
56+
currentOutput5 === targetOutput5,
57+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
58+
);

0 commit comments

Comments
 (0)