|
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 | | -function pad(num){ |
| 4 | +function pad(num) { |
5 | 5 | let stringNum = num.toString(); |
6 | | - if (stringNum.length < 2){ |
7 | | - stringNum = "0" + stringNum; |
8 | | - return stringNum; |
9 | | - } |
10 | | - else{ |
| 6 | + if (stringNum.length < 2) { |
| 7 | + stringNum = "0" + stringNum; |
| 8 | + return stringNum; |
| 9 | + } else { |
11 | 10 | return stringNum; |
12 | 11 | } |
13 | 12 | } |
14 | 13 | function formatAs12HourClock(time) { |
15 | | - if((time.indexOf(":") === -1 && time.indexOf(".") === -1) || (time.indexOf(":") !== -1 && time.indexOf(".") !== -1)){ |
16 | | - return "Not a valid time" // check if there is only either one : or . |
| 14 | + if ( |
| 15 | + (time.indexOf(":") === -1 && time.indexOf(".") === -1) || |
| 16 | + (time.indexOf(":") !== -1 && time.indexOf(".") !== -1) |
| 17 | + ) { |
| 18 | + return "Not a valid time"; // check if there is only either one : or . |
17 | 19 | } |
18 | 20 |
|
19 | | - if(time.indexOf(":") === -1){ |
20 | | - if(time.indexOf(".") !== time.lastIndexOf(".")) |
21 | | - { |
22 | | - return "Not a valid time" //check there is only one . |
| 21 | + if (time.indexOf(":") === -1) { |
| 22 | + if (time.indexOf(".") !== time.lastIndexOf(".")) { |
| 23 | + return "Not a valid time"; //check there is only one . |
23 | 24 | } |
24 | 25 | } |
25 | | - if(time.indexOf(".") === -1){ |
26 | | - if(time.indexOf(":") !== time.lastIndexOf(":")) |
27 | | - { |
28 | | - return "Not a valid time" //check if there is only one : |
| 26 | + if (time.indexOf(".") === -1) { |
| 27 | + if (time.indexOf(":") !== time.lastIndexOf(":")) { |
| 28 | + return "Not a valid time"; //check if there is only one : |
29 | 29 | } |
30 | | - |
31 | 30 | } |
32 | | - |
| 31 | + |
33 | 32 | let colonPeriodIndex = 0; |
34 | | - if(time.indexOf(":") === -1){ |
35 | | - colonPeriodIndex = time.indexOf(".") |
36 | | - } |
37 | | - else |
38 | | - { |
39 | | - colonPeriodIndex = time.indexOf(":") |
| 33 | + if (time.indexOf(":") === -1) { |
| 34 | + colonPeriodIndex = time.indexOf("."); |
| 35 | + } else { |
| 36 | + colonPeriodIndex = time.indexOf(":"); |
40 | 37 | } |
41 | 38 |
|
42 | | - if(time.slice(0, colonPeriodIndex) === "" || time.slice(colonPeriodIndex+1) === ""){ |
43 | | - return "Not a valid time" //check if there is a number before and after time separator |
| 39 | + if ( |
| 40 | + time.slice(0, colonPeriodIndex) === "" || |
| 41 | + time.slice(colonPeriodIndex + 1) === "" |
| 42 | + ) { |
| 43 | + return "Not a valid time"; //check if there is a number before and after time separator |
44 | 44 | } |
45 | 45 | const hours = Number(time.slice(0, colonPeriodIndex)); |
46 | | - const minutes = Number(time.slice(colonPeriodIndex+1)); |
| 46 | + const minutes = Number(time.slice(colonPeriodIndex + 1)); |
47 | 47 | const stringMinutes = pad(minutes); |
48 | 48 | const stringHours = pad(hours); |
49 | | - |
50 | | - if (isNaN(hours) || isNaN(minutes) || hours >= 24 || hours < 0 || minutes >= 60 || minutes < 0){ |
51 | | - return "Not a valid time"; |
52 | | - } //check if numbers are valid |
53 | | - if (hours === 12){ |
| 49 | + |
| 50 | + if ( |
| 51 | + isNaN(hours) || |
| 52 | + isNaN(minutes) || |
| 53 | + hours >= 24 || |
| 54 | + hours < 0 || |
| 55 | + minutes >= 60 || |
| 56 | + minutes < 0 |
| 57 | + ) { |
| 58 | + return "Not a valid time"; |
| 59 | + } //check if numbers are valid |
| 60 | + if (hours === 12) { |
54 | 61 | return `${stringHours}:${stringMinutes} pm`; |
55 | 62 | } |
56 | | - if (hours === 0){ |
| 63 | + if (hours === 0) { |
57 | 64 | return `12:${stringMinutes} am`; |
58 | 65 | } |
59 | 66 | if (hours > 12) { |
60 | 67 | return `${pad(hours - 12)}:${stringMinutes} pm`; |
61 | 68 | } |
62 | | - |
| 69 | + |
63 | 70 | return `${stringHours}:${stringMinutes} am`; |
64 | 71 | } |
65 | 72 |
|
66 | 73 | const currentOutput = formatAs12HourClock("08:00"); |
67 | 74 | const targetOutput = "08:00 am"; |
68 | 75 | console.assert( |
69 | 76 | currentOutput === targetOutput, |
70 | | - `current output: ${currentOutput}, target output: ${targetOutput}` |
| 77 | + `current output: ${currentOutput}, target output: ${targetOutput}`, |
71 | 78 | ); |
72 | 79 |
|
73 | 80 | const currentOutput2 = formatAs12HourClock("23:00"); |
74 | 81 | const targetOutput2 = "11:00 pm"; |
75 | 82 | console.assert( |
76 | 83 | currentOutput2 === targetOutput2, |
77 | | - `current output: ${currentOutput2}, target output: ${targetOutput2}` |
| 84 | + `current output: ${currentOutput2}, target output: ${targetOutput2}`, |
78 | 85 | ); |
79 | 86 |
|
80 | 87 | const currentOutput3 = formatAs12HourClock("23:46"); |
81 | 88 | const targetOutput3 = "11:46 pm"; |
82 | 89 | console.assert( |
83 | 90 | currentOutput3 === targetOutput3, |
84 | | - `current output: ${currentOutput3}, target output: ${targetOutput3}` |
| 91 | + `current output: ${currentOutput3}, target output: ${targetOutput3}`, |
85 | 92 | ); |
86 | 93 |
|
87 | 94 | const currentOutput4 = formatAs12HourClock("12:00"); |
88 | 95 | const targetOutput4 = "12:00 pm"; |
89 | 96 | console.assert( |
90 | 97 | currentOutput4 === targetOutput4, |
91 | | - `current output: ${currentOutput4}, target output: ${targetOutput4}` |
| 98 | + `current output: ${currentOutput4}, target output: ${targetOutput4}`, |
92 | 99 | ); |
93 | 100 |
|
94 | 101 | const currentOutput5 = formatAs12HourClock("00:00"); |
95 | 102 | const targetOutput5 = "12:00 am"; |
96 | 103 | console.assert( |
97 | 104 | currentOutput5 === targetOutput5, |
98 | | - `current output: ${currentOutput5}, target output: ${targetOutput5}` |
| 105 | + `current output: ${currentOutput5}, target output: ${targetOutput5}`, |
99 | 106 | ); |
100 | 107 |
|
101 | 108 | const currentOutput6 = formatAs12HourClock("24:00"); |
102 | 109 | const targetOutput6 = "Not a valid time"; |
103 | 110 | console.assert( |
104 | 111 | currentOutput6 === targetOutput6, |
105 | | - `current output: ${currentOutput6}, target output: ${targetOutput6}` |
| 112 | + `current output: ${currentOutput6}, target output: ${targetOutput6}`, |
106 | 113 | ); |
107 | 114 |
|
108 | 115 | const currentOutput7 = formatAs12HourClock("47:00"); |
109 | 116 | const targetOutput7 = "Not a valid time"; |
110 | 117 | console.assert( |
111 | 118 | currentOutput7 === targetOutput7, |
112 | | - `current output: ${currentOutput7}, target output: ${targetOutput7}` |
| 119 | + `current output: ${currentOutput7}, target output: ${targetOutput7}`, |
113 | 120 | ); |
114 | 121 |
|
115 | 122 | const currentOutput8 = formatAs12HourClock("15:75"); |
116 | 123 | const targetOutput8 = "Not a valid time"; |
117 | 124 | console.assert( |
118 | 125 | currentOutput8 === targetOutput8, |
119 | | - `current output: ${currentOutput8}, target output: ${targetOutput8}` |
| 126 | + `current output: ${currentOutput8}, target output: ${targetOutput8}`, |
120 | 127 | ); |
121 | 128 |
|
122 | 129 | const currentOutput9 = formatAs12HourClock("18:60"); |
123 | 130 | const targetOutput9 = "Not a valid time"; |
124 | 131 | console.assert( |
125 | 132 | currentOutput9 === targetOutput9, |
126 | | - `current output: ${currentOutput9}, target output: ${targetOutput9}` |
| 133 | + `current output: ${currentOutput9}, target output: ${targetOutput9}`, |
127 | 134 | ); |
128 | 135 |
|
129 | 136 | const currentOutput10 = formatAs12HourClock("23:59"); |
130 | 137 | const targetOutput10 = "11:59 pm"; |
131 | 138 | console.assert( |
132 | 139 | currentOutput10 === targetOutput10, |
133 | | - `current output: ${currentOutput10}, target output: ${targetOutput10}` |
| 140 | + `current output: ${currentOutput10}, target output: ${targetOutput10}`, |
134 | 141 | ); |
135 | 142 |
|
136 | 143 | const currentOutput11 = formatAs12HourClock("00:01"); |
137 | 144 | const targetOutput11 = "12:01 am"; |
138 | 145 | console.assert( |
139 | 146 | currentOutput11 === targetOutput11, |
140 | | - `current output: ${currentOutput11}, target output: ${targetOutput11}` |
| 147 | + `current output: ${currentOutput11}, target output: ${targetOutput11}`, |
141 | 148 | ); |
142 | 149 |
|
143 | 150 | const currentOutput12 = formatAs12HourClock("12:01"); |
144 | 151 | const targetOutput12 = "12:01 pm"; |
145 | 152 | console.assert( |
146 | 153 | currentOutput12 === targetOutput12, |
147 | | - `current output: ${currentOutput12}, target output: ${targetOutput12}` |
| 154 | + `current output: ${currentOutput12}, target output: ${targetOutput12}`, |
148 | 155 | ); |
149 | 156 |
|
150 | 157 | const currentOutput13 = formatAs12HourClock("13:00"); |
151 | 158 | const targetOutput13 = "01:00 pm"; |
152 | 159 | console.assert( |
153 | 160 | currentOutput13 === targetOutput13, |
154 | | - `current output: ${currentOutput13}, target output: ${targetOutput13}` |
| 161 | + `current output: ${currentOutput13}, target output: ${targetOutput13}`, |
155 | 162 | ); |
156 | 163 |
|
157 | 164 | const currentOutput14 = formatAs12HourClock("hello"); |
158 | 165 | const targetOutput14 = "Not a valid time"; |
159 | 166 | console.assert( |
160 | 167 | currentOutput14 === targetOutput14, |
161 | | - `current output: ${currentOutput14}, target output: ${targetOutput14}` |
| 168 | + `current output: ${currentOutput14}, target output: ${targetOutput14}`, |
162 | 169 | ); |
163 | 170 |
|
164 | 171 | const currentOutput15 = formatAs12HourClock("15:ab"); |
165 | 172 | const targetOutput15 = "Not a valid time"; |
166 | 173 | console.assert( |
167 | 174 | currentOutput15 === targetOutput15, |
168 | | - `current output: ${currentOutput15}, target output: ${targetOutput15}` |
| 175 | + `current output: ${currentOutput15}, target output: ${targetOutput15}`, |
169 | 176 | ); |
170 | 177 |
|
171 | | - |
172 | 178 | const currentOutput16 = formatAs12HourClock("12"); |
173 | 179 | const targetOutput16 = "Not a valid time"; |
174 | 180 | console.assert( |
175 | 181 | currentOutput16 === targetOutput16, |
176 | | - `current output: ${currentOutput16}, target output: ${targetOutput16}` |
| 182 | + `current output: ${currentOutput16}, target output: ${targetOutput16}`, |
177 | 183 | ); |
178 | 184 |
|
179 | 185 | const currentOutput17 = formatAs12HourClock("12:30abc"); |
180 | 186 | const targetOutput17 = "Not a valid time"; |
181 | 187 | console.assert( |
182 | 188 | currentOutput17 === targetOutput17, |
183 | | - `current output: ${currentOutput17}, target output: ${targetOutput17}` |
| 189 | + `current output: ${currentOutput17}, target output: ${targetOutput17}`, |
184 | 190 | ); |
185 | 191 |
|
186 | 192 | const currentOutput18 = formatAs12HourClock("1.30"); |
187 | 193 | const targetOutput18 = "01:30 am"; |
188 | 194 | console.assert( |
189 | 195 | currentOutput18 === targetOutput18, |
190 | | - `current output: ${currentOutput18}, target output: ${targetOutput18}` |
| 196 | + `current output: ${currentOutput18}, target output: ${targetOutput18}`, |
191 | 197 | ); |
192 | 198 |
|
193 | 199 | const currentOutput19 = formatAs12HourClock("15.30"); |
194 | 200 | const targetOutput19 = "03:30 pm"; |
195 | 201 | console.assert( |
196 | 202 | currentOutput19 === targetOutput19, |
197 | | - `current output: ${currentOutput19}, target output: ${targetOutput19}` |
| 203 | + `current output: ${currentOutput19}, target output: ${targetOutput19}`, |
198 | 204 | ); |
199 | 205 |
|
200 | 206 | const currentOutput20 = formatAs12HourClock("01.30"); |
201 | 207 | const targetOutput20 = "01:30 am"; |
202 | 208 | console.assert( |
203 | 209 | currentOutput20 === targetOutput20, |
204 | | - `current output: ${currentOutput20}, target output: ${targetOutput20}` |
| 210 | + `current output: ${currentOutput20}, target output: ${targetOutput20}`, |
205 | 211 | ); |
206 | 212 |
|
207 | 213 | const currentOutput21 = formatAs12HourClock("12.30:45"); |
208 | 214 | const targetOutput21 = "Not a valid time"; |
209 | 215 | console.assert( |
210 | 216 | currentOutput21 === targetOutput21, |
211 | | - `current output: ${currentOutput21}, target output: ${targetOutput21}` |
| 217 | + `current output: ${currentOutput21}, target output: ${targetOutput21}`, |
212 | 218 | ); |
213 | 219 |
|
214 | | - |
215 | 220 | const currentOutput22 = formatAs12HourClock("12::30"); |
216 | 221 | const targetOutput22 = "Not a valid time"; |
217 | 222 | console.assert( |
218 | 223 | currentOutput22 === targetOutput22, |
219 | | - `current output: ${currentOutput22}, target output: ${targetOutput22}` |
| 224 | + `current output: ${currentOutput22}, target output: ${targetOutput22}`, |
220 | 225 | ); |
221 | 226 |
|
222 | 227 | const currentOutput23 = formatAs12HourClock("12..30"); |
223 | 228 | const targetOutput23 = "Not a valid time"; |
224 | 229 | console.assert( |
225 | 230 | currentOutput23 === targetOutput23, |
226 | | - `current output: ${currentOutput23}, target output: ${targetOutput23}` |
| 231 | + `current output: ${currentOutput23}, target output: ${targetOutput23}`, |
227 | 232 | ); |
228 | 233 |
|
229 | 234 | const currentOutput24 = formatAs12HourClock("12:"); |
230 | 235 | const targetOutput24 = "Not a valid time"; |
231 | 236 | console.assert( |
232 | 237 | currentOutput24 === targetOutput24, |
233 | | - `current output: ${currentOutput24}, target output: ${targetOutput24}` |
| 238 | + `current output: ${currentOutput24}, target output: ${targetOutput24}`, |
234 | 239 | ); |
235 | 240 |
|
236 | 241 | const currentOutput25 = formatAs12HourClock("12."); |
237 | 242 | const targetOutput25 = "Not a valid time"; |
238 | 243 | console.assert( |
239 | 244 | currentOutput25 === targetOutput25, |
240 | | - `current output: ${currentOutput25}, target output: ${targetOutput25}` |
| 245 | + `current output: ${currentOutput25}, target output: ${targetOutput25}`, |
241 | 246 | ); |
242 | 247 |
|
243 | 248 | const currentOutput26 = formatAs12HourClock(":30"); |
244 | 249 | const targetOutput26 = "Not a valid time"; |
245 | 250 | console.assert( |
246 | 251 | currentOutput26 === targetOutput26, |
247 | | - `current output: ${currentOutput26}, target output: ${targetOutput26}` |
| 252 | + `current output: ${currentOutput26}, target output: ${targetOutput26}`, |
248 | 253 | ); |
249 | 254 |
|
250 | 255 | const currentOutput27 = formatAs12HourClock("."); |
251 | 256 | const targetOutput27 = "Not a valid time"; |
252 | 257 | console.assert( |
253 | 258 | currentOutput27 === targetOutput27, |
254 | | - `current output: ${currentOutput27}, target output: ${targetOutput27}` |
| 259 | + `current output: ${currentOutput27}, target output: ${targetOutput27}`, |
255 | 260 | ); |
256 | 261 |
|
257 | 262 | const currentOutput28 = formatAs12HourClock("15:-30"); |
258 | 263 | const targetOutput28 = "Not a valid time"; |
259 | 264 | console.assert( |
260 | 265 | currentOutput28 === targetOutput28, |
261 | | - `current output: ${currentOutput28}, target output: ${targetOutput28}` |
| 266 | + `current output: ${currentOutput28}, target output: ${targetOutput28}`, |
262 | 267 | ); |
263 | 268 |
|
264 | 269 | const currentOutput29 = formatAs12HourClock("-15:30"); |
265 | 270 | const targetOutput29 = "Not a valid time"; |
266 | 271 | console.assert( |
267 | 272 | currentOutput29 === targetOutput29, |
268 | | - `current output: ${currentOutput29}, target output: ${targetOutput29}` |
| 273 | + `current output: ${currentOutput29}, target output: ${targetOutput29}`, |
269 | 274 | ); |
270 | | - |
271 | | - |
|
0 commit comments