|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
| 7 | + |
7 | 8 | if (hours > 12) { |
8 | 9 | return `${hours - 12}:00 pm`; |
9 | 10 | } else if (hours === 12) { |
10 | 11 | return `${hours}:00 pm`; |
11 | 12 | } else if (hours === 0) { |
12 | 13 | return `12:00 am`; |
13 | | - } else if (hours < 12) { |
| 14 | + } else { |
14 | 15 | return `${time} am`; |
15 | | - } else return `${time} am`; |
16 | | -} |
| 16 | + } |
| 17 | + |
| 18 | + const currentOutput = formatAs12HourClock("08:00"); |
| 19 | + const targetOutput = "08:00 am"; |
| 20 | + console.assert( |
| 21 | + currentOutput === targetOutput, |
| 22 | + `current output: ${currentOutput}, target output: ${targetOutput}` |
| 23 | + ); |
17 | 24 |
|
18 | | -const currentOutput = formatAs12HourClock("08:00"); |
19 | | -const targetOutput = "08:00 am"; |
20 | | -console.assert( |
21 | | - currentOutput === targetOutput, |
22 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
23 | | -); |
| 25 | + const currentOutput2 = formatAs12HourClock("23:00"); |
| 26 | + const targetOutput2 = "11:00 pm"; |
| 27 | + console.assert( |
| 28 | + currentOutput2 === targetOutput2, |
| 29 | + `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 30 | + ); |
24 | 31 |
|
25 | | -const currentOutput2 = formatAs12HourClock("23:00"); |
26 | | -const targetOutput2 = "11:00 pm"; |
27 | | -console.assert( |
28 | | - currentOutput2 === targetOutput2, |
29 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
30 | | -); |
| 32 | + const currentOutput3 = formatAs12HourClock("00:00"); |
| 33 | + const targetOutput3 = "12:00 am"; |
| 34 | + console.assert( |
| 35 | + currentOutput3 === targetOutput3, |
| 36 | + `current output: ${currentOutput3}, target output: ${targetOutput3}` |
| 37 | + ); |
31 | 38 |
|
32 | | -const currentOutput3 = formatAs12HourClock("00:00"); |
33 | | -const targetOutput3 = "12:00 am"; |
34 | | -console.assert( |
35 | | - currentOutput3 === targetOutput3, |
36 | | - `current output: ${currentOutput3}, target output: ${targetOutput3}` |
37 | | -); |
| 39 | + const currentOutput4 = formatAs12HourClock("12:00"); |
| 40 | + const targetOutput4 = "12:00 pm"; |
| 41 | + console.assert( |
| 42 | + currentOutput4 === targetOutput4, |
| 43 | + `current output: ${currentOutput4}, target output: ${targetOutput4}` |
| 44 | + ); |
38 | 45 |
|
39 | | -const currentOutput4 = formatAs12HourClock("12:00"); |
40 | | -const targetOutput4 = "12:00 pm"; |
41 | | -console.assert( |
42 | | - currentOutput4 === targetOutput4, |
43 | | - `current output: ${currentOutput4}, target output: ${targetOutput4}` |
44 | | -); |
| 46 | + const currentOutput5 = formatAs12HourClock("00:01"); |
| 47 | + const targetOutput5 = "12:01 am"; |
| 48 | + console.assert( |
| 49 | + currentOutput5 === targetOutput5, |
| 50 | + `current output: ${currentOutput5}, target output: ${targetOutput5}` |
| 51 | + ); |
45 | 52 |
|
46 | | -const currentOutput5 = formatAs12HourClock("00:01"); |
47 | | -const targetOutput5 = "12:01 am"; |
48 | | -console.assert( |
49 | | - currentOutput5 === targetOutput5, |
50 | | - `current output: ${currentOutput5}, target output: ${targetOutput5}` |
51 | | -); |
| 53 | + const currentOutput6 = formatAs12HourClock("12:01"); |
| 54 | + const targetOutput6 = "12:01 pm"; |
| 55 | + console.assert( |
| 56 | + currentOutput6 === targetOutput6, |
| 57 | + `current output: ${currentOutput6}, target output: ${targetOutput6}` |
| 58 | + ); |
| 59 | + |
| 60 | + const currentOutput7 = formatAs12HourClock("23:59"); |
| 61 | + const targetOutput7 = "11:59 pm"; |
| 62 | + console.assert( |
| 63 | + currentOutput7 === targetOutput7, |
| 64 | + `current output: ${currentOutput7}, target output: ${targetOutput7}` |
| 65 | + ); |
| 66 | + |
| 67 | + const currentOutput8 = formatAs12HourClock("13:00"); |
| 68 | + const targetOutput8 = "01:00 pm"; |
| 69 | + console.assert( |
| 70 | + currentOutput8 === targetOutput8, |
| 71 | + `current output: ${currentOutput8}, target output: ${targetOutput8}` |
| 72 | + ); |
| 73 | +} |
52 | 74 |
|
53 | | -const currentOutput6 = formatAs12HourClock("12:01"); |
54 | | -const targetOutput6 = "12:01 pm"; |
55 | | -console.assert( |
56 | | - currentOutput6 === targetOutput6, |
57 | | - `current output: ${currentOutput6}, target output: ${targetOutput6}` |
58 | | -); |
| 75 | +// Edge test cases that should be tested: |
59 | 76 |
|
60 | | -const currentOutput7 = formatAs12HourClock("23:59"); |
61 | | -const targetOutput7 = "11:59 pm"; |
62 | | -console.assert( |
63 | | - currentOutput7 === targetOutput7, |
64 | | - `current output: ${currentOutput7}, target output: ${targetOutput7}` |
65 | | -); |
| 77 | +// 1) 08:00 -> 08:00 am |
| 78 | +// 2) 23:00 -> 11:00 PM |
| 79 | +// 3) 00:00 -> 12:00 AM |
| 80 | +// 5) 12:00 -> 12:00 PM |
| 81 | +// 6) 00:01 -> 12:01 AM |
| 82 | +// 7) 12:01 -> 12:01 PM |
| 83 | +// 8) 23:59 -> 11:59 PM |
| 84 | +// 9) 13:00 -> 1:00 PM |
66 | 85 |
|
67 | | -const currentOutput8 = formatAs12HourClock("13:00"); |
68 | | -const targetOutput8 = "01:00 pm"; |
69 | | -console.assert( |
70 | | - currentOutput8 === targetOutput8, |
71 | | - `current output: ${currentOutput8}, target output: ${targetOutput8}` |
72 | | -); |
|
0 commit comments