|
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 | +//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`; |
| 11 | +//} |
| 12 | + |
| 13 | +//const currentOutput = formatAs12HourClock("08:00"); |
| 14 | +//const targetOutput = "08:00 am"; |
| 15 | +//console.assert( |
| 16 | + //currentOutput === targetOutput, |
| 17 | + //`current output: ${currentOutput}, target output: ${targetOutput}` |
| 18 | +//); |
| 19 | + |
| 20 | +//const currentOutput2 = formatAs12HourClock("23:00"); |
| 21 | +//const targetOutput2 = "11:00 pm"; |
| 22 | +//console.assert( |
| 23 | + //currentOutput2 === targetOutput2, |
| 24 | + //`current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 25 | +//); |
| 26 | + |
| 27 | +// Bug 1: Minutes are hardcoded for PM. It always uses :00 for PM. If the input is "23:45", the output becomes "11:00 pm". |
| 28 | +// Bug 2: AM hours are notconverted to 12 hour format. It works for "8:00" but "00:30" becomes "00:30" when it should be "12:30 am". |
| 29 | +// Bug 3: 12PM and 12AM are not handled correctly. "12:00" becomes "12:00 am" whjen it should be "12:00 pm", and "00:00" becomes "00:00 am" when it should be "12:00 am". |
| 30 | + |
5 | 31 | function formatAs12HourClock(time) { |
6 | | - const hours = Number(time.slice(0, 2)); |
7 | | - if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 32 | + let [hours, minutes] = time.split(":"); |
| 33 | + hours = Number(hours); |
| 34 | + let period = "am"; |
| 35 | + |
| 36 | + if (hours === 0) { |
| 37 | + hours = 12; // midnight |
| 38 | + } else if (hours === 12) { |
| 39 | + period = "pm"; // noon |
| 40 | + } else if (hours > 12) { |
| 41 | + hours -= 12; |
| 42 | + period = "pm"; |
9 | 43 | } |
10 | | - return `${time} am`; |
| 44 | + |
| 45 | + const paddedHours = hours.toString().padStart(2, "0"); |
| 46 | + |
| 47 | + return `${paddedHours}:${minutes} ${period}`; |
11 | 48 | } |
12 | 49 |
|
13 | | -const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
15 | | -console.assert( |
16 | | - currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
18 | | -); |
19 | | - |
20 | | -const currentOutput2 = formatAs12HourClock("23:00"); |
21 | | -const targetOutput2 = "11:00 pm"; |
22 | | -console.assert( |
23 | | - currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
25 | | -); |
| 50 | +//testing the function with various inputs. |
| 51 | +console.log(formatAs12HourClock("08:00")); // 08:00 am |
| 52 | +console.log(formatAs12HourClock("23:00")); // 11:00 pm |
| 53 | +console.log(formatAs12HourClock("00:30")); // 12:30 am |
| 54 | +console.log(formatAs12HourClock("12:15")); // 12:15 pm |
| 55 | +console.log(formatAs12HourClock("13:45")); // 01:45 pm |
| 56 | +console.log(formatAs12HourClock("11:59")); // 11:59 am |
| 57 | +console.log(formatAs12HourClock("12:00")); // 12:00 pm |
0 commit comments