|
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 | | - const hours = Number(time.slice(0, 2)); |
| 6 | + const hours = 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 `${Number(hours) - 12}:${minutes} pm`; |
| 11 | + } else if (Number(hours) === 0) { |
| 12 | + return `12:${minutes} am`; |
9 | 13 | } |
10 | | - return `${time} am`; |
| 14 | + return `${hours}:${minutes} am`; |
11 | 15 | } |
12 | 16 |
|
13 | 17 | //Test cases |
14 | 18 | const currentOutput = formatAs12HourClock("08:00"); |
15 | 19 | const targetOutput = "08:00 am"; |
16 | | -console.assert(currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}`); |
| 20 | +console.assert( |
| 21 | + currentOutput === targetOutput, |
| 22 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 23 | +); |
17 | 24 |
|
18 | 25 | const currentOutput2 = formatAs12HourClock("23:00"); |
19 | 26 | const targetOutput2 = "11:00 pm"; |
20 | | -console.assert(currentOutput2 === targetOutput2,`current output: ${currentOutput2}, target output: ${targetOutput2}`); |
21 | | - |
| 27 | +console.assert( |
| 28 | + currentOutput2 === targetOutput2, |
| 29 | + `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 30 | +); |
22 | 31 |
|
23 | 32 | const currentOutput3 = formatAs12HourClock("00:00"); |
24 | 33 | const targetOutput3 = "12:00 am"; |
25 | | -console.assert(currentOutput3 === targetOutput3,`current output: ${currentOutput3}, target output: ${targetOutput3}`); |
26 | | - |
27 | | - |
28 | | -const currentOutput4= formatAs12HourClock("00:00"); |
29 | | -const targetOutput4= "12:00 am"; |
30 | | -console.assert(currentOutput4 === targetOutput4,`current output: ${currentOutput4}, target output: ${targetOutput4}`); |
31 | | - |
32 | | -const currentOutput5= formatAs12HourClock("15:01"); |
33 | | -const targetOutput5= "3:01 pm"; |
34 | | -console.assert(currentOutput5 === targetOutput5,`current output: ${currentOutput5}, target output: ${targetOutput5}`); |
| 34 | +console.assert( |
| 35 | + currentOutput3 === targetOutput3, |
| 36 | + `current output: ${currentOutput3}, target output: ${targetOutput3}` |
| 37 | +); |
| 38 | + |
| 39 | +const currentOutput5 = formatAs12HourClock("15:01"); |
| 40 | +const targetOutput5 = "3:01 pm"; |
| 41 | +console.assert( |
| 42 | + currentOutput5 === targetOutput5, |
| 43 | + `current output: ${currentOutput5}, target output: ${targetOutput5}` |
| 44 | +); |
| 45 | + |
| 46 | +const currentOutput6 = formatAs12HourClock("15:01"); |
| 47 | +const targetOutput6 = "3:01 pm"; |
| 48 | +console.assert( |
| 49 | + currentOutput6 === targetOutput6, |
| 50 | + `current output: ${currentOutput6}, target output: ${targetOutput6}` |
| 51 | +); |
0 commit comments