Skip to content

Commit 95ce71d

Browse files
completed the format-time.js stretch task
1 parent 5dcb8b5 commit 95ce71d

1 file changed

Lines changed: 61 additions & 5 deletions

File tree

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,78 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = time.slice(3, 5);
8+
79
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
10+
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
11+
}
12+
if (hours === 12) {
13+
return `${hours}:${minutes.toString().padStart(2, "0")} pm`;
14+
}
15+
if (hours === 0) {
16+
return `12:${minutes.toString().padStart(2, "0")} am`;
917
}
10-
return `${time} am`;
18+
return `${hours}:${minutes.toString().padStart(2, "0")} am`;
1119
}
1220

1321
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
22+
const targetOutput = "8: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}`,
33+
);
34+
const currentOutput3 = formatAs12HourClock("15:30");
35+
const targetOutput3 = "3:30 pm";
36+
console.assert(
37+
currentOutput3 === targetOutput3,
38+
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
39+
);
40+
const currentOutput4 = formatAs12HourClock("12:10");
41+
const targetOutput4 = "12:10 pm";
42+
console.assert(
43+
currentOutput4 === targetOutput4,
44+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
45+
);
46+
const currentOutput5 = formatAs12HourClock("08:45");
47+
const targetOutput5 = "8:45 am";
48+
console.assert(
49+
currentOutput5 === targetOutput5,
50+
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
51+
);
52+
const currentOutput6 = formatAs12HourClock("09:05");
53+
const targetOutput6 = "9:05 am";
54+
console.assert(
55+
currentOutput6 === targetOutput6,
56+
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
57+
);
58+
const currentOutput7 = formatAs12HourClock("17:45");
59+
const targetOutput7 = "5:45 pm";
60+
console.assert(
61+
currentOutput7 === targetOutput7,
62+
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
63+
);
64+
const currentOutput8 = formatAs12HourClock("00:00");
65+
const targetOutput8 = "12:00 am";
66+
console.assert(
67+
currentOutput8 === targetOutput8,
68+
`current output: ${currentOutput8}, target output: ${targetOutput8}`,
69+
);
70+
const currentOutput9 = formatAs12HourClock("11:59");
71+
const targetOutput9 = "11:59 am";
72+
console.assert(
73+
currentOutput9 === targetOutput9,
74+
`current output: ${currentOutput9}, target output: ${targetOutput9}`,
75+
);
76+
const currentOutput10 = formatAs12HourClock("23:59");
77+
const targetOutput10 = "11:59 pm";
78+
console.assert(
79+
currentOutput10 === targetOutput10,
80+
`current output: ${currentOutput10}, target output: ${targetOutput10}`,
2581
);

0 commit comments

Comments
 (0)