Skip to content

Commit 2e7a6a3

Browse files
author
russom
committed
Edge test cases for 24 to 12 hours formatting functions
1 parent 45bd5ef commit 2e7a6a3

2 files changed

Lines changed: 65 additions & 51 deletions

File tree

Project-CLI-Treasure-Hunt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 3e66462a26c1d68cc2ac562b0d3ad7b3c2210fa1

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

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,82 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
78
if (hours > 12) {
89
return `${hours - 12}:00 pm`;
910
} else if (hours === 12) {
1011
return `${hours}:00 pm`;
1112
} else if (hours === 0) {
1213
return `12:00 am`;
13-
} else if (hours < 12) {
14+
} else {
1415
return `${time} am`;
15-
} else return `${time} am`;
16-
}
16+
}
17+
18+
const currentOutput = formatAs12HourClock("08:00");
19+
const targetOutput = "08:00 am";
20+
console.assert(
21+
currentOutput === targetOutput,
22+
`current output: ${currentOutput}, target output: ${targetOutput}`
23+
);
1724

18-
const currentOutput = formatAs12HourClock("08:00");
19-
const targetOutput = "08:00 am";
20-
console.assert(
21-
currentOutput === targetOutput,
22-
`current output: ${currentOutput}, target output: ${targetOutput}`
23-
);
25+
const currentOutput2 = formatAs12HourClock("23:00");
26+
const targetOutput2 = "11:00 pm";
27+
console.assert(
28+
currentOutput2 === targetOutput2,
29+
`current output: ${currentOutput2}, target output: ${targetOutput2}`
30+
);
2431

25-
const currentOutput2 = formatAs12HourClock("23:00");
26-
const targetOutput2 = "11:00 pm";
27-
console.assert(
28-
currentOutput2 === targetOutput2,
29-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
30-
);
32+
const currentOutput3 = formatAs12HourClock("00:00");
33+
const targetOutput3 = "12:00 am";
34+
console.assert(
35+
currentOutput3 === targetOutput3,
36+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
37+
);
3138

32-
const currentOutput3 = formatAs12HourClock("00:00");
33-
const targetOutput3 = "12:00 am";
34-
console.assert(
35-
currentOutput3 === targetOutput3,
36-
`current output: ${currentOutput3}, target output: ${targetOutput3}`
37-
);
39+
const currentOutput4 = formatAs12HourClock("12:00");
40+
const targetOutput4 = "12:00 pm";
41+
console.assert(
42+
currentOutput4 === targetOutput4,
43+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
44+
);
3845

39-
const currentOutput4 = formatAs12HourClock("12:00");
40-
const targetOutput4 = "12:00 pm";
41-
console.assert(
42-
currentOutput4 === targetOutput4,
43-
`current output: ${currentOutput4}, target output: ${targetOutput4}`
44-
);
46+
const currentOutput5 = formatAs12HourClock("00:01");
47+
const targetOutput5 = "12:01 am";
48+
console.assert(
49+
currentOutput5 === targetOutput5,
50+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
51+
);
4552

46-
const currentOutput5 = formatAs12HourClock("00:01");
47-
const targetOutput5 = "12:01 am";
48-
console.assert(
49-
currentOutput5 === targetOutput5,
50-
`current output: ${currentOutput5}, target output: ${targetOutput5}`
51-
);
53+
const currentOutput6 = formatAs12HourClock("12:01");
54+
const targetOutput6 = "12:01 pm";
55+
console.assert(
56+
currentOutput6 === targetOutput6,
57+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
58+
);
59+
60+
const currentOutput7 = formatAs12HourClock("23:59");
61+
const targetOutput7 = "11:59 pm";
62+
console.assert(
63+
currentOutput7 === targetOutput7,
64+
`current output: ${currentOutput7}, target output: ${targetOutput7}`
65+
);
66+
67+
const currentOutput8 = formatAs12HourClock("13:00");
68+
const targetOutput8 = "01:00 pm";
69+
console.assert(
70+
currentOutput8 === targetOutput8,
71+
`current output: ${currentOutput8}, target output: ${targetOutput8}`
72+
);
73+
}
5274

53-
const currentOutput6 = formatAs12HourClock("12:01");
54-
const targetOutput6 = "12:01 pm";
55-
console.assert(
56-
currentOutput6 === targetOutput6,
57-
`current output: ${currentOutput6}, target output: ${targetOutput6}`
58-
);
75+
// Edge test cases that should be tested:
5976

60-
const currentOutput7 = formatAs12HourClock("23:59");
61-
const targetOutput7 = "11:59 pm";
62-
console.assert(
63-
currentOutput7 === targetOutput7,
64-
`current output: ${currentOutput7}, target output: ${targetOutput7}`
65-
);
77+
// 1) 08:00 -> 08:00 am
78+
// 2) 23:00 -> 11:00 PM
79+
// 3) 00:00 -> 12:00 AM
80+
// 5) 12:00 -> 12:00 PM
81+
// 6) 00:01 -> 12:01 AM
82+
// 7) 12:01 -> 12:01 PM
83+
// 8) 23:59 -> 11:59 PM
84+
// 9) 13:00 -> 1:00 PM
6685

67-
const currentOutput8 = formatAs12HourClock("13:00");
68-
const targetOutput8 = "01:00 pm";
69-
console.assert(
70-
currentOutput8 === targetOutput8,
71-
`current output: ${currentOutput8}, target output: ${targetOutput8}`
72-
);

0 commit comments

Comments
 (0)