|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
| 7 | + const minutes = time.slice(3, 5); |
7 | 8 | if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 9 | + return `${hours - 12}:${minutes} pm`; |
9 | 10 | } |
10 | 11 | if (hours === 12) { |
11 | | - return `${hours}:00 pm`; |
| 12 | + return `12:${minutes} pm`; |
12 | 13 | } |
13 | 14 | if (hours === 0) { |
14 | | - const minutes = time.slice(3, 5); |
15 | 15 | return `12:${minutes} am`; |
16 | 16 | } |
17 | 17 | return `${time} am`; |
@@ -48,3 +48,21 @@ console.assert( |
48 | 48 | currentOutput5 === targetOutput5, |
49 | 49 | `current output: ${currentOutput5}, target output: ${targetOutput5}` |
50 | 50 | ); |
| 51 | +const currentOutput6 = formatAs12HourClock("12:35"); |
| 52 | +const targetOutput6 = "12:35 pm"; |
| 53 | +console.assert( |
| 54 | + currentOutput6 === targetOutput6, |
| 55 | + `current output: ${currentOutput6}, target output: ${targetOutput6}` |
| 56 | +); |
| 57 | +const currentOutput7 = formatAs12HourClock("01:01"); |
| 58 | +const targetOutput7 = "01:01 am"; |
| 59 | +console.assert( |
| 60 | + currentOutput7 === targetOutput7, |
| 61 | + `current output: ${currentOutput7}, target output: ${targetOutput7}` |
| 62 | +); |
| 63 | +const currentOutput8 = formatAs12HourClock("13:01"); |
| 64 | +const targetOutput8 = "1:01 pm"; |
| 65 | +console.assert( |
| 66 | + currentOutput8 === targetOutput8, |
| 67 | + `current output: ${currentOutput8}, target output: ${targetOutput8}` |
| 68 | +); |
0 commit comments