|
2 | 2 | // Make sure to do the prep before you do the coursework |
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 | +/* |
| 6 | +edge cases to be checked: |
| 7 | +1. 00:00 |
| 8 | +2. 24:00 |
| 9 | +3. 12:00 |
| 10 | +4. 12:01 |
| 11 | +5. 00:01 |
| 12 | +*/ |
| 13 | + |
| 14 | +function pad(num) { |
| 15 | + return num.toString().padStart(2, "0"); |
| 16 | +} |
| 17 | + |
5 | 18 | function formatAs12HourClock(time) { |
6 | 19 | const hours = Number(time.slice(0, 2)); |
7 | | - if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
9 | | - } |
10 | | - return `${time} am`; |
| 20 | + const minutes = Number(time.slice(3, 5)); |
| 21 | + |
| 22 | + if (hours == 0 || hours == 24) { |
| 23 | + return `12:${pad(minutes)} am`; |
| 24 | + } else if (hours > 12) { |
| 25 | + return `${pad(hours - 12)}:00 pm`; |
| 26 | + } else if (hours == 12) { |
| 27 | + return `${time} pm`; |
| 28 | + } else return `${time} am`; |
11 | 29 | } |
12 | 30 |
|
13 | | -const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
| 31 | +let currentOutput = formatAs12HourClock("08:00"); |
| 32 | +let targetOutput = "08:00 am"; |
| 33 | +console.assert( |
| 34 | + currentOutput === targetOutput, |
| 35 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 36 | +); |
| 37 | + |
| 38 | +currentOutput = formatAs12HourClock("23:00"); |
| 39 | +targetOutput = "11:00 pm"; |
| 40 | +console.assert( |
| 41 | + currentOutput === targetOutput, |
| 42 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 43 | +); |
| 44 | + |
| 45 | +currentOutput = formatAs12HourClock("13:00"); |
| 46 | +targetOutput = "01:00 pm"; |
| 47 | +console.assert( |
| 48 | + currentOutput === targetOutput, |
| 49 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 50 | +); |
| 51 | + |
| 52 | +currentOutput = formatAs12HourClock("00:00"); |
| 53 | +targetOutput = "12:00 am"; |
| 54 | +console.assert( |
| 55 | + currentOutput === targetOutput, |
| 56 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 57 | +); |
| 58 | + |
| 59 | +currentOutput = formatAs12HourClock("24:00"); |
| 60 | +targetOutput = "12:00 am"; |
| 61 | +console.assert( |
| 62 | + currentOutput === targetOutput, |
| 63 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 64 | +); |
| 65 | + |
| 66 | +currentOutput = formatAs12HourClock("12:00"); |
| 67 | +targetOutput = "12:00 pm"; |
15 | 68 | console.assert( |
16 | 69 | currentOutput === targetOutput, |
17 | 70 | `current output: ${currentOutput}, target output: ${targetOutput}` |
18 | 71 | ); |
19 | 72 |
|
20 | | -const currentOutput2 = formatAs12HourClock("23:00"); |
21 | | -const targetOutput2 = "11:00 pm"; |
| 73 | +currentOutput = formatAs12HourClock("12:01"); |
| 74 | +targetOutput = "12:01 pm"; |
22 | 75 | console.assert( |
23 | | - currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 76 | + currentOutput === targetOutput, |
| 77 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 78 | +); |
| 79 | + |
| 80 | +currentOutput = formatAs12HourClock("00:01"); |
| 81 | +targetOutput = "12:01 am"; |
| 82 | +console.assert( |
| 83 | + currentOutput === targetOutput, |
| 84 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
25 | 85 | ); |
0 commit comments