|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
7 | | - if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 7 | + const minutes = time.slice(3); |
| 8 | + if (hours > 24) { |
| 9 | + return `${hours - 24}:${minutes} am +1`; |
| 10 | + } else if (hours > 12) { |
| 11 | + return `${hours - 12}:${minutes} pm`; |
9 | 12 | } |
10 | | - return `${time} am`; |
| 13 | + return `${hours}:${minutes} am`; |
11 | 14 | } |
12 | 15 |
|
13 | | -const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
| 16 | +const currentOutput = formatAs12HourClock("08:01"); |
| 17 | +const targetOutput = "8:01 am"; |
15 | 18 | console.assert( |
16 | 19 | currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 20 | + `current output: ${currentOutput}, target output: ${targetOutput}`, |
18 | 21 | ); |
19 | 22 |
|
20 | 23 | const currentOutput2 = formatAs12HourClock("23:00"); |
21 | 24 | const targetOutput2 = "11:00 pm"; |
22 | 25 | console.assert( |
23 | 26 | currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 27 | + `current output: ${currentOutput2}, target output: ${targetOutput2}`, |
25 | 28 | ); |
| 29 | + |
| 30 | +const currentOutput3 = formatAs12HourClock("25:00"); |
| 31 | +const targetOutput3 = "1:00 am +1"; |
| 32 | +console.assert( |
| 33 | + currentOutput3 === targetOutput3, |
| 34 | + `current output: ${currentOutput3}, target output: ${targetOutput3}`, |
| 35 | +); |
| 36 | + |
| 37 | +console.log(formatAs12HourClock("08:01")); |
| 38 | +console.log(formatAs12HourClock("23:55")); |
| 39 | +console.log(formatAs12HourClock("25:01")); |
| 40 | + |
| 41 | +//Two patches |
| 42 | +//First hour input > 24, current algo is hours - 12 , e.g. 25-12 and print 13:00pm , rather than 1:00am (+1) |
| 43 | +//to Define `hoursOfOneDay` = 24 |
| 44 | +//2 min input |
| 45 | +//to Define |
0 commit comments