Skip to content

Commit c197bdc

Browse files
committed
Updated const minutes = time.slice(3) --- to const minutes = time.slice(-2) and explained hours = hours % 12 || 12;
1 parent 46f9c56 commit c197bdc

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ console.assert(
6262

6363
function formatAs12HourClock(time) {
6464
let hours = Number(time.slice(0, 2));
65-
const minutes = time.slice(3);
65+
const minutes = time.slice(-2);
6666

6767
let suffix;
6868
if (hours >= 12) {
@@ -71,7 +71,11 @@ function formatAs12HourClock(time) {
7171
suffix = "am";
7272
}
7373

74-
hours = hours % 12 || 12; // convert 0 to 12, 13 to 1
74+
hours = hours % 12 || 12; // convert 0 to 12, 13 to 1 it returns the remainder
75+
// for eg. taking 12/12 the remainder is 0
76+
// similarly if 13 is taken 13/12 , the remainder is 1, the operator || helps us to return the remainder after
77+
// a division
78+
7579
const formattedHours = hours.toString().padStart(2, "0");
7680

7781
return `${formattedHours}:${minutes} ${suffix}`;
@@ -84,3 +88,6 @@ console.assert(
8488
currentOutput6 === targetOutput6,
8589
`current output: ${currentOutput6}, target output: ${targetOutput6}`
8690
)
91+
92+
93+
// Updated const minutes = time.slice(3) --- to const minutes = time.slice(-2)

0 commit comments

Comments
 (0)