Skip to content

Commit eb8e26b

Browse files
committed
fixed a bug in the function, should return 12:37. and should return
1 parent fa69127 commit eb8e26b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
function formatAs12HourClock(time) {
66
let hours = time.slice(0, 2);
77
let minutes= time.slice(3, 5);
8+
let timeIndicator;
89

9-
if (Number(hours) > 12) {
10+
if (Number(hours) >= 12) {
1011
hours = String(hours - 12);
1112
hours = hours.padStart(2, "0");
12-
13-
return `${hours}:${minutes} pm`;
13+
timeIndicator = "pm";
14+
} else {
15+
timeIndicator = "am";
1416
}
15-
return `${hours}:${minutes} am`;
17+
if ( hours === "00")
18+
hours = "12";
19+
20+
return `${hours}:${minutes} ${timeIndicator}`;
1621
}
1722

1823
let currentOutput = formatAs12HourClock("14:00");
@@ -37,7 +42,14 @@ console.assert(
3742
);
3843
//==========================
3944
currentOutput = formatAs12HourClock("00:37");
40-
targetOutput = "00:37 am";
45+
targetOutput = "12:37 am";
46+
console.assert(
47+
currentOutput === targetOutput,
48+
`current output: ${currentOutput}, target output: ${targetOutput}`
49+
);
50+
//==========================
51+
currentOutput = formatAs12HourClock("12:37");
52+
targetOutput = "12:37 pm";
4153
console.assert(
4254
currentOutput === targetOutput,
4355
`current output: ${currentOutput}, target output: ${targetOutput}`

0 commit comments

Comments
 (0)