Skip to content

Commit c8fe831

Browse files
committed
Format-time exercise Done
1 parent 3edc12c commit c8fe831

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,47 @@
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

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
6+
let hours = time.slice(0, 2);
7+
if (Number(hours) > 12) {
8+
hours = String(hours - 12);
9+
hours = hours.padStart(2, "0");
10+
11+
return `${hours}:00 pm`;
912
}
1013
return `${time} am`;
1114
}
1215

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
16+
let currentOutput = formatAs12HourClock("14:00");
17+
let targetOutput = "02:00 pm";
1518
console.assert(
1619
currentOutput === targetOutput,
1720
`current output: ${currentOutput}, target output: ${targetOutput}`
1821
);
19-
20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22+
//==========================
23+
currentOutput = formatAs12HourClock("23:00");
24+
targetOutput = "11:00 pm";
25+
console.assert(
26+
currentOutput === targetOutput,
27+
`current output: ${currentOutput}, target output: ${targetOutput}`
28+
);
29+
//==========================
30+
currentOutput = formatAs12HourClock("07:00");
31+
targetOutput = "07:00 am";
32+
console.assert(
33+
currentOutput === targetOutput,
34+
`current output: ${currentOutput}, target output: ${targetOutput}`
35+
);
36+
//==========================
37+
currentOutput = formatAs12HourClock("00:00");
38+
targetOutput = "00:00 am";
39+
console.assert(
40+
currentOutput === targetOutput,
41+
`current output: ${currentOutput}, target output: ${targetOutput}`
42+
);
43+
//==========================
44+
currentOutput = formatAs12HourClock("20:00");
45+
targetOutput = "08:00 pm";
2246
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
47+
currentOutput === targetOutput,
48+
`current output: ${currentOutput}, target output: ${targetOutput}`
2549
);

0 commit comments

Comments
 (0)