Skip to content

Commit 5eb1ab8

Browse files
committed
Refactor formatAs12HourClock to reduce code duplication
Moved padStart(2, "0") to a single location after calculating rawHour. Improves readability and makes the function easier to maintain. Thanks for the helpful suggestion!
1 parent a9c2a21 commit 5eb1ab8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@ function formatAs12HourClock(time) {
22
const hours = Number(time.slice(0, 2));
33
const minutes = time.slice(3, 5);
44

5-
let formattedHour;
5+
let rawHour;
66
let period;
77

88
if (hours === 0) {
9-
formattedHour = "12";
9+
rawHour = 12;
1010
period = "am";
1111
} else if (hours === 12) {
12-
formattedHour = "12";
12+
rawHour = 12;
1313
period = "pm";
1414
} else if (hours > 12) {
15-
formattedHour = String(hours - 12).padStart(2, "0");
15+
rawHour = hours - 12;
1616
period = "pm";
1717
} else {
18-
formattedHour = String(hours).padStart(2, "0");
18+
rawHour = hours;
1919
period = "am";
2020
}
2121

22+
const formattedHour = String(rawHour).padStart(2, "0");
23+
2224
return `${formattedHour}:${minutes} ${period}`;
2325
}
2426

0 commit comments

Comments
 (0)