Skip to content

Commit 29fa555

Browse files
Fix formatting of minutes and stored in a const variable to be reused in template literals
1 parent b33ee89 commit 29fa555

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
77
const minutes = time.slice(3, 5);
8+
const formattedMinutes = minutes.padStart(2, "0");
89

910
if (hours > 12) {
10-
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
11+
return `${hours - 12}:${formattedMinutes} pm`;
1112
}
1213
if (hours === 12) {
13-
return `${hours}:${minutes.toString().padStart(2, "0")} pm`;
14+
return `${hours}:${formattedMinutes} pm`;
1415
}
1516
if (hours === 0) {
16-
return `12:${minutes.toString().padStart(2, "0")} am`;
17+
return `12:${formattedMinutes} am`;
1718
}
18-
return `${hours}:${minutes.toString().padStart(2, "0")} am`;
19+
return `${hours}:${formattedMinutes} am`;
1920
}
2021

2122
const currentOutput = formatAs12HourClock("08:00");

0 commit comments

Comments
 (0)