|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
| 7 | + const minutes = time.slice(3, 5); |
| 8 | + |
7 | 9 | 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`; |
9 | 17 | } |
10 | | - return `${time} am`; |
| 18 | + return `${hours}:${minutes.toString().padStart(2, "0")} am`; |
11 | 19 | } |
12 | 20 |
|
13 | 21 | const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
| 22 | +const targetOutput = "8:00 am"; |
15 | 23 | console.assert( |
16 | 24 | currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 25 | + `current output: ${currentOutput}, target output: ${targetOutput}`, |
18 | 26 | ); |
19 | 27 |
|
20 | 28 | const currentOutput2 = formatAs12HourClock("23:00"); |
21 | 29 | const targetOutput2 = "11:00 pm"; |
22 | 30 | console.assert( |
23 | 31 | 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}`, |
25 | 81 | ); |
0 commit comments