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