Skip to content

Commit 5eba71d

Browse files
author
russom
committed
Add if condition to compare time
1 parent 6bece94 commit 5eba71d

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
8+
if (hours === 0) {
9+
return `12${time.slice(2)} am`;
10+
}
11+
12+
if (hours === 12) {
13+
return `${time} pm`;
14+
}
715
if (hours > 12) {
816
return `${hours - 12}:00 pm`;
917
}
@@ -14,18 +22,18 @@ const currentOutput = formatAs12HourClock("08:00");
1422
const targetOutput = "08:00 am";
1523
console.assert(
1624
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
25+
`current output: ${currentOutput}, target output: ${targetOutput}`,
1826
);
1927

2028
const currentOutput2 = formatAs12HourClock("23:00");
2129
const targetOutput2 = "11:00 pm";
2230
console.assert(
2331
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
32+
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
2533
);
2634

27-
const currentOutput3 = formatAs12HourClock("24:00");
28-
const targetOutput3 = "00:00 pm";
35+
const currentOutput3 = formatAs12HourClock("00:00");
36+
const targetOutput3 = "12:00 am";
2937
console.assert(
3038
currentOutput3 === targetOutput3,
3139
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
@@ -37,3 +45,10 @@ console.assert(
3745
currentOutput4 === targetOutput4,
3846
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
3947
);
48+
49+
const currentOutput4 = formatAs12HourClock("12:00");
50+
const targetOutput4 = "12:00 pm";
51+
console.assert(
52+
currentOutput4 === targetOutput4,
53+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
54+
);

0 commit comments

Comments
 (0)