|
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 | + // Extract hours as a Number and minutes as a String |
6 | 7 | const hours = Number(time.slice(0, 2)); |
7 | | - if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 8 | + const minutes = time.slice(3, 5); |
| 9 | + |
| 10 | + let formattedHours; |
| 11 | + let period; |
| 12 | + |
| 13 | + // Decision Tree for 12-Hour Conversion |
| 14 | + if (hours === 0) { |
| 15 | + formattedHours = "12"; |
| 16 | + period = "am"; |
| 17 | + } else if (hours === 12) { |
| 18 | + formattedHours = "12"; |
| 19 | + period = "pm"; |
| 20 | + } else if (hours > 12) { |
| 21 | + const twelveHour = hours - 12; |
| 22 | + formattedHours = twelveHour < 10 ? `0${twelveHour}` : `${twelveHour}`; |
| 23 | + period = "pm"; |
| 24 | + } else { |
| 25 | + formattedHours = time.slice(0, 2); |
| 26 | + period = "am"; |
9 | 27 | } |
10 | | - return `${time} am`; |
| 28 | + |
| 29 | + // Formatting: Reassemble hours, minutes, and period |
| 30 | + return `${formattedHours}:${minutes} ${period}`; |
11 | 31 | } |
12 | 32 |
|
| 33 | +// Test Cases |
| 34 | + |
13 | 35 | const currentOutput = formatAs12HourClock("08:00"); |
14 | 36 | const targetOutput = "08:00 am"; |
15 | 37 | console.assert( |
16 | 38 | currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 39 | + `current ${currentOutput}, target ${targetOutput}` |
18 | 40 | ); |
19 | 41 |
|
20 | 42 | const currentOutput2 = formatAs12HourClock("23:00"); |
21 | 43 | const targetOutput2 = "11:00 pm"; |
22 | 44 | console.assert( |
23 | 45 | currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 46 | + `current ${currentOutput2}, target ${targetOutput2}` |
| 47 | +); |
| 48 | + |
| 49 | +const currentOutput3 = formatAs12HourClock("00:00"); |
| 50 | +const targetOutput3 = "12:00 am"; |
| 51 | +console.assert( |
| 52 | + currentOutput3 === targetOutput3, |
| 53 | + `current ${currentOutput3}, target ${targetOutput3}` |
| 54 | +); |
| 55 | + |
| 56 | +const currentOutput4 = formatAs12HourClock("01:00"); |
| 57 | +const targetOutput4 = "01:00 am"; |
| 58 | +console.assert( |
| 59 | + currentOutput4 === targetOutput4, |
| 60 | + `current ${currentOutput4}, target ${targetOutput4}` |
25 | 61 | ); |
| 62 | + |
| 63 | +const currentOutput5 = formatAs12HourClock("12:00"); |
| 64 | +const targetOutput5 = "12:00 pm"; |
| 65 | +console.assert( |
| 66 | + currentOutput5 === targetOutput5, |
| 67 | + `current ${currentOutput5}, target ${targetOutput5}` |
| 68 | +); |
| 69 | + |
| 70 | +const currentOutput6 = formatAs12HourClock("13:00"); |
| 71 | +const targetOutput6 = "01:00 pm"; |
| 72 | +console.assert( |
| 73 | + currentOutput6 === targetOutput6, |
| 74 | + `current ${currentOutput6}, target ${targetOutput6}` |
| 75 | +); |
| 76 | + |
| 77 | +const currentOutput7 = formatAs12HourClock("15:30"); |
| 78 | +const targetOutput7 = "03:30 pm"; |
| 79 | +console.assert( |
| 80 | + currentOutput7 === targetOutput7, |
| 81 | + `current ${currentOutput7}, target ${targetOutput7}` |
| 82 | +); |
| 83 | + |
| 84 | +const currentOutput8 = formatAs12HourClock("23:59"); |
| 85 | +const targetOutput8 = "11:59 pm"; |
| 86 | +console.assert( |
| 87 | + currentOutput8 === targetOutput8, |
| 88 | + `current ${currentOutput8}, target ${targetOutput8}` |
| 89 | +); |
| 90 | + |
| 91 | +console.log(formatAs12HourClock("00:00")); |
| 92 | +console.log(formatAs12HourClock("01:00")); |
| 93 | +console.log(formatAs12HourClock("12:00")); |
| 94 | +console.log(formatAs12HourClock("13:00")); |
| 95 | +console.log(formatAs12HourClock("15:30")); |
| 96 | +console.log(formatAs12HourClock("23:59")); |
| 97 | +console.log("All assertions completed!"); |
0 commit comments