Skip to content

Commit c4affdd

Browse files
author
AlanGit-debug2604
committed
formatTime.js formatAs12HourClock function to correctly handle hour inputs over 24 and update test cases for clarity, also handle minutes input other than :00
1 parent 0142183 commit c4affdd

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,42 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
7+
const minutes = time.slice(3);
8+
if (hours > 24) {
9+
return `${hours - 24}:${minutes} am +1`;
10+
} else if (hours > 12) {
11+
return `${hours - 12}:${minutes} pm`;
912
}
10-
return `${time} am`;
13+
return `${hours}:${minutes} am`;
1114
}
1215

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
16+
const currentOutput = formatAs12HourClock("08:01");
17+
const targetOutput = "8:01 am";
1518
console.assert(
1619
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
20+
`current output: ${currentOutput}, target output: ${targetOutput}`,
1821
);
1922

2023
const currentOutput2 = formatAs12HourClock("23:00");
2124
const targetOutput2 = "11:00 pm";
2225
console.assert(
2326
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
27+
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
2528
);
29+
30+
const currentOutput3 = formatAs12HourClock("25:00");
31+
const targetOutput3 = "1:00 am +1";
32+
console.assert(
33+
currentOutput3 === targetOutput3,
34+
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
35+
);
36+
37+
console.log(formatAs12HourClock("08:01"));
38+
console.log(formatAs12HourClock("23:55"));
39+
console.log(formatAs12HourClock("25:01"));
40+
41+
//Two patches
42+
//First hour input > 24, current algo is hours - 12 , e.g. 25-12 and print 13:00pm , rather than 1:00am (+1)
43+
//to Define `hoursOfOneDay` = 24
44+
//2 min input
45+
//to Define

0 commit comments

Comments
 (0)