|
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`; |
9 | | - } |
10 | | - return `${time} am`; |
| 6 | + const hours = time.slice(0, 2); |
| 7 | + const mins = time.slice(3); |
| 8 | + if (hours > 12) { |
| 9 | + const hoursIsPastMidday = hours - 12; |
| 10 | + return `${hoursIsPastMidday}:${mins}pm` |
| 11 | + } else if (hours === "12") { |
| 12 | + return `${hours}:${mins}pm` |
| 13 | + }else if (hours === "00"){ |
| 14 | + return `12:${mins}am` |
| 15 | + } |
| 16 | + return `${time}am` |
11 | 17 | } |
12 | 18 |
|
13 | 19 | const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
| 20 | +const targetOutput = "08:00am"; |
15 | 21 | console.assert( |
16 | | - currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 22 | + currentOutput === targetOutput, |
| 23 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
18 | 24 | ); |
19 | 25 |
|
20 | 26 | const currentOutput2 = formatAs12HourClock("23:00"); |
21 | | -const targetOutput2 = "11:00 pm"; |
| 27 | +const targetOutput2 = "11:00pm"; |
22 | 28 | console.assert( |
23 | | - currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 29 | + currentOutput2 === targetOutput2, |
| 30 | + `current output: ${currentOutput2}, target output: ${targetOutput2}` |
25 | 31 | ); |
| 32 | + |
| 33 | +const currentOutput3 = formatAs12HourClock("00:00"); |
| 34 | +const targetOutput3 = "12:00am"; |
| 35 | +console.assert( |
| 36 | + currentOutput3 === targetOutput3, |
| 37 | + `current output: ${currentOutput3}, target output: ${targetOutput3}` |
| 38 | +); |
| 39 | + |
| 40 | +const currentOutput4 = formatAs12HourClock("17:45"); |
| 41 | +const targetOutPut4 = "5:45pm"; |
| 42 | +console.assert( |
| 43 | + currentOutput4 === targetOutPut4, |
| 44 | + `current output: ${currentOutput4}, target output: ${targetOutPut4}` |
| 45 | +); |
| 46 | + |
| 47 | +const currentOutput5 = formatAs12HourClock("00:01"); |
| 48 | +const targetOutPut5= "12:01am"; |
| 49 | +console.assert( |
| 50 | + currentOutput5 === targetOutPut5, |
| 51 | + `current output: ${currentOutput5}, target output: ${targetOutPut5}` |
| 52 | +); |
| 53 | + |
| 54 | +const currentOutput6 = formatAs12HourClock("00:59"); |
| 55 | +const targetOutPut6= "12:59am"; |
| 56 | +console.assert( |
| 57 | + currentOutput6 === targetOutPut6, |
| 58 | + `current output: ${currentOutput6}, target output: ${targetOutPut6}` |
| 59 | +) |
| 60 | + |
| 61 | +const currentOutput7 = formatAs12HourClock("12:00"); |
| 62 | +const targetOutPut7= "12:00pm"; |
| 63 | +console.assert( |
| 64 | + currentOutput7 === targetOutPut7, |
| 65 | + `current output: ${currentOutput7}, target output: ${targetOutPut7}` |
| 66 | +) |
0 commit comments