|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
7 | | - const minutes = time.slice(3, 5); |
| 7 | + const minutes = time.slice(-2); |
8 | 8 | if (hours > 12) { |
9 | | - return `${hours - 12}:${minutes} pm`; |
| 9 | + return `${String(hours - 12).padStart(2, "0")}:${minutes}pm`; |
10 | 10 | } else if (hours == 12) { |
11 | | - return `12:${minutes} pm`; |
| 11 | + return `12:${minutes}pm`; |
12 | 12 | } else if (hours === 0) { |
13 | | - return `12:${minutes} am`; |
| 13 | + return `12:${minutes}am`; |
14 | 14 | } |
15 | | - return `${String(hours).padStart(2, "0")}:${minutes} am`; |
| 15 | + return `${String(hours).padStart(2, "0")}:${minutes}am`; |
16 | 16 | } |
17 | 17 | let currentOutput = formatAs12HourClock("00:01"); |
18 | | -let targetOutput = "12:01 am"; |
| 18 | +let targetOutput = "12:01am"; |
19 | 19 | console.assert( |
20 | 20 | currentOutput === targetOutput, |
21 | 21 | `current output: ${currentOutput}, target output: ${targetOutput}` |
22 | 22 | ); |
23 | 23 |
|
24 | 24 | const currentOutput2 = formatAs12HourClock("11:59"); |
25 | | -const targetOutput2 = "11:59 am"; |
| 25 | +const targetOutput2 = "11:59am"; |
26 | 26 | console.assert( |
27 | 27 | currentOutput2 === targetOutput2, |
28 | 28 | `current output: ${currentOutput2}, target output: ${targetOutput2}` |
29 | 29 | ); |
30 | 30 | const currentOutput3 = formatAs12HourClock("12:00"); |
31 | | -const targetOutput3 = "12:00 pm"; |
| 31 | +const targetOutput3 = "12:00pm"; |
32 | 32 | console.assert( |
33 | 33 | currentOutput3 === targetOutput3, |
34 | 34 | `current output: ${currentOutput3}, target output: ${targetOutput3}` |
35 | 35 | ); |
36 | 36 | currentOutput = formatAs12HourClock("00:00"); |
37 | | -targetOutput = "12:00 am"; |
| 37 | +targetOutput = "12:00am"; |
38 | 38 | console.assert( |
39 | 39 | currentOutput === targetOutput, |
40 | 40 | `current output: ${currentOutput}, target output: ${targetOutput}` |
41 | 41 | ); |
42 | 42 | currentOutput = formatAs12HourClock("04:12"); |
43 | | -targetOutput = "04:12 am"; |
| 43 | +targetOutput = "04:12am"; |
| 44 | +console.assert( |
| 45 | + currentOutput === targetOutput, |
| 46 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 47 | +); |
| 48 | +currentOutput = formatAs12HourClock("16:12"); |
| 49 | +targetOutput = "04:12pm"; |
44 | 50 | console.assert( |
45 | 51 | currentOutput === targetOutput, |
46 | 52 | `current output: ${currentOutput}, target output: ${targetOutput}` |
|
0 commit comments