Skip to content

Commit c688a68

Browse files
wrote test cases for edge cases and updated the function accordingly
1 parent 57ad058 commit c688a68

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes=time.slice(3,5)
8+
if(hours<1){
9+
return `12:${minutes} am`
10+
}
11+
if(hours==12&& minutes==00){
12+
return "12:00 pm"
13+
}
714
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
15+
return `${hours - 12}:${minutes} pm`;
916
}
1017
return `${time} am`;
1118
}
@@ -23,3 +30,34 @@ console.assert(
2330
currentOutput2 === targetOutput2,
2431
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2532
);
33+
34+
const currentOutput3 = formatAs12HourClock("23:33");
35+
const targetOutput3 = "11:33 pm";
36+
console.assert(
37+
currentOutput3 === targetOutput3,
38+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
39+
);
40+
41+
const currentOutput4 = formatAs12HourClock("11:59");
42+
const targetOutput4 = "11:59 am";
43+
console.assert(
44+
currentOutput4 === targetOutput4,
45+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
46+
);
47+
48+
const currentOutput5 = formatAs12HourClock("00:01");
49+
const targetOutput5 = "12:01 am";
50+
console.assert(
51+
currentOutput5 === targetOutput5,
52+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
53+
);
54+
55+
const currentOutput6 = formatAs12HourClock("12:00");
56+
const targetOutput6 = "12:00 pm";
57+
console.assert(
58+
currentOutput6 === targetOutput6,
59+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
60+
);
61+
62+
63+

0 commit comments

Comments
 (0)