|
1 | 1 | // This is the latest solution to the problem from the prep. |
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 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
| 7 | + const minutes = time.slice(3, 5); |
| 8 | + |
| 9 | + if (hours === 0) { |
| 10 | + return `12:${minutes} am`; |
| 11 | + } |
| 12 | + |
| 13 | + if (hours === 12) { |
| 14 | + return `12:${minutes} pm`; |
| 15 | + } |
| 16 | + |
7 | 17 | if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 18 | + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; |
9 | 19 | } |
| 20 | + |
10 | 21 | return `${time} am`; |
11 | 22 | } |
| 23 | +console.log(formatAs12HourClock("00:00")); |
| 24 | +console.log(formatAs12HourClock("01:00")); |
| 25 | +console.log(formatAs12HourClock("11:00")); |
| 26 | +console.log(formatAs12HourClock("12:00")); |
| 27 | +console.log(formatAs12HourClock("13:00")); |
| 28 | +console.log(formatAs12HourClock("23:00")); |
12 | 29 |
|
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 | | -); |
|
0 commit comments