Skip to content

Commit e5372e2

Browse files
committed
Fixed the format-time function
1 parent d3bb259 commit e5372e2

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@
22
// Make sure to do the prep before you do the coursework
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

5+
// function formatAs12HourClock(time) {
6+
// const hours = Number(time.slice(0, 2));
7+
// if (hours > 12) {
8+
// return `${hours - 12}:00 pm`;
9+
// }
10+
// return `${time} am`;
11+
// }
12+
513
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
9-
}
10-
return `${time} am`;
14+
let hours = Number(time.slice(0, 2));
15+
let minutes = Number(time.slice(3, 5));
16+
let ampm = hours >= 12 ? 'pm' : 'am';
17+
hours = hours % 12;
18+
hours = hours ? hours : 12; // the hour '0' should be '12'
19+
minutes = minutes < 10 ? '0'+minutes : minutes;
20+
21+
return `${hours}:${minutes} ${ampm}`
1122
}
1223

24+
console.log(formatAs12HourClock("00:00"));
25+
console.log(formatAs12HourClock("23:59"));
26+
console.log(formatAs12HourClock("12:00"));
27+
console.log(formatAs12HourClock("21:20"));
28+
29+
1330
const currentOutput = formatAs12HourClock("08:00");
1431
const targetOutput = "08:00 am";
1532
console.assert(

0 commit comments

Comments
 (0)