|
3 | 3 | // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) {
|
| 6 | + if (!time.includes(':')){ |
| 7 | + time = time.slice(0,-2)+':' + time.slice(-2) |
| 8 | + } |
6 | 9 | const hours = Number(time.slice(0, 2));
|
7 | 10 | if (hours > 12) {
|
8 |
| - return `${hours - 12}:00 pm`; |
| 11 | + const minutes = time.slice(3); |
| 12 | + return `${hours - 12}:${minutes} pm`; |
| 13 | + }else if (time.length < 5){ |
| 14 | + time = time.padStart(5,'0') |
9 | 15 | }
|
10 | 16 | return `${time} am`;
|
11 | 17 | }
|
12 | 18 |
|
13 | 19 | const currentOutput = formatAs12HourClock("08:00");
|
14 | 20 | const targetOutput = "08:00 am";
|
15 |
| -console.assert( |
16 |
| - currentOutput === targetOutput, |
17 |
| - `current output: ${currentOutput}, target output: ${targetOutput}` |
18 |
| -); |
| 21 | +console.assert(currentOutput === targetOutput,`current output1: ${currentOutput}, target output: ${targetOutput}`); |
| 22 | + |
| 23 | +const currentOutput2 = formatAs12HourClock("23:01"); |
| 24 | +const targetOutput2 = "11:01 pm"; |
| 25 | +console.assert(currentOutput2 === targetOutput2,`current output2: ${currentOutput2}, target output: ${targetOutput2}`); |
| 26 | + |
| 27 | +const currentOutput3 = formatAs12HourClock("07:34"); |
| 28 | +const targetOutput3 = "07:34 am"; |
| 29 | +console.assert(currentOutput3 === targetOutput3,`current output3: ${currentOutput3}, target output: ${targetOutput3}`); |
| 30 | + |
| 31 | +const currentOutput4 = formatAs12HourClock("1100"); |
| 32 | +const targetOutput4 = ("11:00 am"); |
| 33 | +console.assert(currentOutput4 === targetOutput4,`current output4: ${currentOutput4}, target output: ${targetOutput4}`); |
| 34 | + |
| 35 | +const currentOutput5 = formatAs12HourClock("8:24"); |
| 36 | +const targetOutput5 = "08:24 am"; |
| 37 | +console.assert(currentOutput5 === targetOutput5,`current output5: ${currentOutput5}, target output: ${targetOutput5}`); |
19 | 38 |
|
20 |
| -const currentOutput2 = formatAs12HourClock("23:00"); |
21 |
| -const targetOutput2 = "11:00 pm"; |
22 |
| -console.assert( |
23 |
| - currentOutput2 === targetOutput2, |
24 |
| - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
25 |
| -); |
| 39 | +const currentOutput6 = formatAs12HourClock("1:01"); |
| 40 | +const targetOutput6 = "01:01 am"; |
| 41 | +console.assert(currentOutput6 === targetOutput6,`current output6: ${currentOutput6}, target output: ${targetOutput6}`); |
0 commit comments