|
4 | 4 |
|
5 | 5 | function formatAs12HourClock(time) { |
6 | 6 | const hours = Number(time.slice(0, 2)); |
7 | | - if (hours > 12) { |
8 | | - return `${hours - 12}:00 pm`; |
| 7 | + const minutes = Number(time.slice(3, 5)); |
| 8 | + |
| 9 | + if (hours < 12) { |
| 10 | + return `${time} am`; |
| 11 | + } else if (hours == 12) { |
| 12 | + return `${time} am`; |
| 13 | + } else if (hours == 24) { |
| 14 | + return `12:00 am`; |
| 15 | + } |
| 16 | + |
| 17 | + if (hours > 12 && hours < 22 && minutes < 10) { |
| 18 | + return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`; |
| 19 | + } |
| 20 | + if (hours > 12 && hours < 22 && minutes >=10) { |
| 21 | + return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`; |
| 22 | + } |
| 23 | + if (hours >= 22 && minutes < 10) { |
| 24 | + return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`; |
| 25 | + } |
| 26 | + if (hours >= 22 && minutes >= 10) { |
| 27 | + return `${hours - 12}:${minutes} pm`; |
9 | 28 | } |
10 | | - return `${time} am`; |
11 | 29 | } |
12 | 30 |
|
13 | | -const currentOutput = formatAs12HourClock("08:00"); |
14 | | -const targetOutput = "08:00 am"; |
| 31 | +const currentOutput = formatAs12HourClock("01:00"); |
| 32 | +const targetOutput = "01:00 am"; |
15 | 33 | console.assert( |
16 | 34 | currentOutput === targetOutput, |
17 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 35 | + `current output: ${currentOutput}, target output: ${targetOutput}`, |
18 | 36 | ); |
19 | 37 |
|
20 | 38 | const currentOutput2 = formatAs12HourClock("23:00"); |
21 | 39 | const targetOutput2 = "11:00 pm"; |
22 | 40 | console.assert( |
23 | 41 | currentOutput2 === targetOutput2, |
24 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 42 | + `current output: ${currentOutput2}, target output: ${targetOutput2}`, |
| 43 | +); |
| 44 | + |
| 45 | +const currentOutput3 = formatAs12HourClock("00:00"); |
| 46 | +const targetOutput3 = "00:00 am"; |
| 47 | +console.assert( |
| 48 | + currentOutput3 === targetOutput3, |
| 49 | + `current output: ${currentOutput3}, target output: ${targetOutput3}`, |
| 50 | +); |
| 51 | + |
| 52 | +const currentOutput4 = formatAs12HourClock("23:59"); // ***Asserrion failed : This needs fixing if hours > 12 minutes are not tracked and replaced by 00. =>fixec |
| 53 | +// ); |
| 54 | +const targetOutput4 = "11:59 pm"; |
| 55 | +console.assert( |
| 56 | + currentOutput4 === targetOutput4, |
| 57 | + `current output: ${currentOutput4}, target output: ${targetOutput4}`, |
| 58 | +); |
| 59 | + |
| 60 | +const currentOutput5 = formatAs12HourClock("12:00"); |
| 61 | +const targetOutput5 = "12:00 am"; |
| 62 | +console.assert( |
| 63 | + currentOutput5 === targetOutput5, |
| 64 | + `current output: ${currentOutput5}, target output: ${targetOutput5}`, // ***Asserrion failed : This needs fixing => fixed |
| 65 | +); |
| 66 | + |
| 67 | +const currentOutput6 = formatAs12HourClock("24:00"); // ***Asserrion failed : This needs fixing =>fixed |
| 68 | +const targetOutput6 = "12:00 am"; |
| 69 | +console.assert( |
| 70 | + currentOutput6 === targetOutput6, |
| 71 | + `current output: ${currentOutput6}, target output: ${targetOutput6}`, |
| 72 | +); |
| 73 | + |
| 74 | +const currentOutput7 = formatAs12HourClock("12:01"); // ***Asserrion failed : This needs fixing =>fixed |
| 75 | +const targetOutput7 = "12:01 am"; |
| 76 | +console.assert( |
| 77 | + currentOutput7 === targetOutput7, |
| 78 | + `current output: ${currentOutput7}, target output: ${targetOutput7}`, |
25 | 79 | ); |
| 80 | + |
| 81 | +const currentOutput8 = formatAs12HourClock("22:10"); // ***Asserrion failed : This needs fixing =>fixed |
| 82 | +const targetOutput8 = "01:10 am"; |
| 83 | +console.assert( |
| 84 | + currentOutput8=== targetOutput8, |
| 85 | + `current output: ${currentOutput8}, target output: ${targetOutput8}`, |
| 86 | +); |
| 87 | + |
| 88 | +const currentOutput9 = formatAs12HourClock("22:10"); // ***Asserrion failed : This needs fixing =>fixed |
| 89 | +const targetOutput9 = "10:10 am"; |
| 90 | +console.assert( |
| 91 | + currentOutput9 === targetOutput9, |
| 92 | + `current output: ${currentOutput9}, target output: ${targetOutput9}`, |
| 93 | +); |
| 94 | + |
0 commit comments