Skip to content

Commit 4f46ec2

Browse files
committed
corrections have been made.
1 parent 550232a commit 4f46ec2

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,36 @@ console.assert(
3838
`current output: ${currentOutput4}, target output: ${targetOutput4}`
3939
);
4040

41+
const currentOutput5 = formatAs12HourClock("14:30");
42+
const targetOutput5 = "2:30 pm";
43+
console.assert(
44+
currentOutput5 === targetOutput5,
45+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
46+
);
47+
4148
console.log(formatAs12HourClock("08:00"));
4249
console.log(formatAs12HourClock("23:00"));
4350
console.log(formatAs12HourClock("00:00"));
4451
console.log(formatAs12HourClock("12:00"));
52+
console.log(formatAs12HourClock("14:30"));
4553

4654
//The function did not handle the edge cases 00:00 and 12:00.
55+
// The correct function should be like this
56+
function formatAs12HourClock(time) {
57+
const hours = Number(time.slice(0, 2));
58+
const minutes = time.slice(2);
59+
60+
if (hours === 0) {
61+
return `12${minutes} am`;
62+
}
63+
64+
if (hours === 12) {
65+
return `12${minutes} pm`;
66+
}
67+
68+
if (hours > 12) {
69+
return `${hours - 12}${minutes} pm`;
70+
}
71+
72+
return `${time} am`;
73+
}

0 commit comments

Comments
 (0)