Skip to content

Commit a6b3a00

Browse files
committed
fix refactor function to reduce repeated slicing
1 parent 4bc9f9f commit a6b3a00

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = Number(time.slice(3, 5)).toString().padStart(2, "0");
78
if (hours === 0 || hours === 24) {
8-
return `12:${time.slice(3, 5)} am`;
9+
return `12:${minutes} am`;
910
} else if (hours > 12) {
10-
return `${(hours - 12).toString().padStart(2, "0")}:${time.slice(3, 5)} pm`;
11+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`;
1112
} else if (hours < 12) {
12-
return `${time.slice(0, 2)}:${time.slice(3, 5)} am`;
13+
return `${hours.toString().padStart(2, "0")}:${minutes} am`;
1314
} else {
14-
return `${time.slice(0, 2)}:${time.slice(3, 5)} pm`;
15+
return `${hours.toString().padStart(2, "0")}:${minutes} pm`;
1516
}
1617
}
1718

0 commit comments

Comments
 (0)