Skip to content

Commit ab74a01

Browse files
committed
Fix formatting
1 parent 38825cb commit ab74a01

1 file changed

Lines changed: 70 additions & 67 deletions

File tree

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 70 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,271 +1,274 @@
11
// This is the latest solution to the problem from the prep.
22
// Make sure to do the prep before you do the coursework
33
// 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) {
55
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 {
1110
return stringNum;
1211
}
1312
}
1413
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 .
1719
}
1820

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 .
2324
}
2425
}
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 :
2929
}
30-
3130
}
32-
31+
3332
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(":");
4037
}
4138

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
4444
}
4545
const hours = Number(time.slice(0, colonPeriodIndex));
46-
const minutes = Number(time.slice(colonPeriodIndex+1));
46+
const minutes = Number(time.slice(colonPeriodIndex + 1));
4747
const stringMinutes = pad(minutes);
4848
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) {
5461
return `${stringHours}:${stringMinutes} pm`;
5562
}
56-
if (hours === 0){
63+
if (hours === 0) {
5764
return `12:${stringMinutes} am`;
5865
}
5966
if (hours > 12) {
6067
return `${pad(hours - 12)}:${stringMinutes} pm`;
6168
}
62-
69+
6370
return `${stringHours}:${stringMinutes} am`;
6471
}
6572

6673
const currentOutput = formatAs12HourClock("08:00");
6774
const targetOutput = "08:00 am";
6875
console.assert(
6976
currentOutput === targetOutput,
70-
`current output: ${currentOutput}, target output: ${targetOutput}`
77+
`current output: ${currentOutput}, target output: ${targetOutput}`,
7178
);
7279

7380
const currentOutput2 = formatAs12HourClock("23:00");
7481
const targetOutput2 = "11:00 pm";
7582
console.assert(
7683
currentOutput2 === targetOutput2,
77-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
84+
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
7885
);
7986

8087
const currentOutput3 = formatAs12HourClock("23:46");
8188
const targetOutput3 = "11:46 pm";
8289
console.assert(
8390
currentOutput3 === targetOutput3,
84-
`current output: ${currentOutput3}, target output: ${targetOutput3}`
91+
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
8592
);
8693

8794
const currentOutput4 = formatAs12HourClock("12:00");
8895
const targetOutput4 = "12:00 pm";
8996
console.assert(
9097
currentOutput4 === targetOutput4,
91-
`current output: ${currentOutput4}, target output: ${targetOutput4}`
98+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
9299
);
93100

94101
const currentOutput5 = formatAs12HourClock("00:00");
95102
const targetOutput5 = "12:00 am";
96103
console.assert(
97104
currentOutput5 === targetOutput5,
98-
`current output: ${currentOutput5}, target output: ${targetOutput5}`
105+
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
99106
);
100107

101108
const currentOutput6 = formatAs12HourClock("24:00");
102109
const targetOutput6 = "Not a valid time";
103110
console.assert(
104111
currentOutput6 === targetOutput6,
105-
`current output: ${currentOutput6}, target output: ${targetOutput6}`
112+
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
106113
);
107114

108115
const currentOutput7 = formatAs12HourClock("47:00");
109116
const targetOutput7 = "Not a valid time";
110117
console.assert(
111118
currentOutput7 === targetOutput7,
112-
`current output: ${currentOutput7}, target output: ${targetOutput7}`
119+
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
113120
);
114121

115122
const currentOutput8 = formatAs12HourClock("15:75");
116123
const targetOutput8 = "Not a valid time";
117124
console.assert(
118125
currentOutput8 === targetOutput8,
119-
`current output: ${currentOutput8}, target output: ${targetOutput8}`
126+
`current output: ${currentOutput8}, target output: ${targetOutput8}`,
120127
);
121128

122129
const currentOutput9 = formatAs12HourClock("18:60");
123130
const targetOutput9 = "Not a valid time";
124131
console.assert(
125132
currentOutput9 === targetOutput9,
126-
`current output: ${currentOutput9}, target output: ${targetOutput9}`
133+
`current output: ${currentOutput9}, target output: ${targetOutput9}`,
127134
);
128135

129136
const currentOutput10 = formatAs12HourClock("23:59");
130137
const targetOutput10 = "11:59 pm";
131138
console.assert(
132139
currentOutput10 === targetOutput10,
133-
`current output: ${currentOutput10}, target output: ${targetOutput10}`
140+
`current output: ${currentOutput10}, target output: ${targetOutput10}`,
134141
);
135142

136143
const currentOutput11 = formatAs12HourClock("00:01");
137144
const targetOutput11 = "12:01 am";
138145
console.assert(
139146
currentOutput11 === targetOutput11,
140-
`current output: ${currentOutput11}, target output: ${targetOutput11}`
147+
`current output: ${currentOutput11}, target output: ${targetOutput11}`,
141148
);
142149

143150
const currentOutput12 = formatAs12HourClock("12:01");
144151
const targetOutput12 = "12:01 pm";
145152
console.assert(
146153
currentOutput12 === targetOutput12,
147-
`current output: ${currentOutput12}, target output: ${targetOutput12}`
154+
`current output: ${currentOutput12}, target output: ${targetOutput12}`,
148155
);
149156

150157
const currentOutput13 = formatAs12HourClock("13:00");
151158
const targetOutput13 = "01:00 pm";
152159
console.assert(
153160
currentOutput13 === targetOutput13,
154-
`current output: ${currentOutput13}, target output: ${targetOutput13}`
161+
`current output: ${currentOutput13}, target output: ${targetOutput13}`,
155162
);
156163

157164
const currentOutput14 = formatAs12HourClock("hello");
158165
const targetOutput14 = "Not a valid time";
159166
console.assert(
160167
currentOutput14 === targetOutput14,
161-
`current output: ${currentOutput14}, target output: ${targetOutput14}`
168+
`current output: ${currentOutput14}, target output: ${targetOutput14}`,
162169
);
163170

164171
const currentOutput15 = formatAs12HourClock("15:ab");
165172
const targetOutput15 = "Not a valid time";
166173
console.assert(
167174
currentOutput15 === targetOutput15,
168-
`current output: ${currentOutput15}, target output: ${targetOutput15}`
175+
`current output: ${currentOutput15}, target output: ${targetOutput15}`,
169176
);
170177

171-
172178
const currentOutput16 = formatAs12HourClock("12");
173179
const targetOutput16 = "Not a valid time";
174180
console.assert(
175181
currentOutput16 === targetOutput16,
176-
`current output: ${currentOutput16}, target output: ${targetOutput16}`
182+
`current output: ${currentOutput16}, target output: ${targetOutput16}`,
177183
);
178184

179185
const currentOutput17 = formatAs12HourClock("12:30abc");
180186
const targetOutput17 = "Not a valid time";
181187
console.assert(
182188
currentOutput17 === targetOutput17,
183-
`current output: ${currentOutput17}, target output: ${targetOutput17}`
189+
`current output: ${currentOutput17}, target output: ${targetOutput17}`,
184190
);
185191

186192
const currentOutput18 = formatAs12HourClock("1.30");
187193
const targetOutput18 = "01:30 am";
188194
console.assert(
189195
currentOutput18 === targetOutput18,
190-
`current output: ${currentOutput18}, target output: ${targetOutput18}`
196+
`current output: ${currentOutput18}, target output: ${targetOutput18}`,
191197
);
192198

193199
const currentOutput19 = formatAs12HourClock("15.30");
194200
const targetOutput19 = "03:30 pm";
195201
console.assert(
196202
currentOutput19 === targetOutput19,
197-
`current output: ${currentOutput19}, target output: ${targetOutput19}`
203+
`current output: ${currentOutput19}, target output: ${targetOutput19}`,
198204
);
199205

200206
const currentOutput20 = formatAs12HourClock("01.30");
201207
const targetOutput20 = "01:30 am";
202208
console.assert(
203209
currentOutput20 === targetOutput20,
204-
`current output: ${currentOutput20}, target output: ${targetOutput20}`
210+
`current output: ${currentOutput20}, target output: ${targetOutput20}`,
205211
);
206212

207213
const currentOutput21 = formatAs12HourClock("12.30:45");
208214
const targetOutput21 = "Not a valid time";
209215
console.assert(
210216
currentOutput21 === targetOutput21,
211-
`current output: ${currentOutput21}, target output: ${targetOutput21}`
217+
`current output: ${currentOutput21}, target output: ${targetOutput21}`,
212218
);
213219

214-
215220
const currentOutput22 = formatAs12HourClock("12::30");
216221
const targetOutput22 = "Not a valid time";
217222
console.assert(
218223
currentOutput22 === targetOutput22,
219-
`current output: ${currentOutput22}, target output: ${targetOutput22}`
224+
`current output: ${currentOutput22}, target output: ${targetOutput22}`,
220225
);
221226

222227
const currentOutput23 = formatAs12HourClock("12..30");
223228
const targetOutput23 = "Not a valid time";
224229
console.assert(
225230
currentOutput23 === targetOutput23,
226-
`current output: ${currentOutput23}, target output: ${targetOutput23}`
231+
`current output: ${currentOutput23}, target output: ${targetOutput23}`,
227232
);
228233

229234
const currentOutput24 = formatAs12HourClock("12:");
230235
const targetOutput24 = "Not a valid time";
231236
console.assert(
232237
currentOutput24 === targetOutput24,
233-
`current output: ${currentOutput24}, target output: ${targetOutput24}`
238+
`current output: ${currentOutput24}, target output: ${targetOutput24}`,
234239
);
235240

236241
const currentOutput25 = formatAs12HourClock("12.");
237242
const targetOutput25 = "Not a valid time";
238243
console.assert(
239244
currentOutput25 === targetOutput25,
240-
`current output: ${currentOutput25}, target output: ${targetOutput25}`
245+
`current output: ${currentOutput25}, target output: ${targetOutput25}`,
241246
);
242247

243248
const currentOutput26 = formatAs12HourClock(":30");
244249
const targetOutput26 = "Not a valid time";
245250
console.assert(
246251
currentOutput26 === targetOutput26,
247-
`current output: ${currentOutput26}, target output: ${targetOutput26}`
252+
`current output: ${currentOutput26}, target output: ${targetOutput26}`,
248253
);
249254

250255
const currentOutput27 = formatAs12HourClock(".");
251256
const targetOutput27 = "Not a valid time";
252257
console.assert(
253258
currentOutput27 === targetOutput27,
254-
`current output: ${currentOutput27}, target output: ${targetOutput27}`
259+
`current output: ${currentOutput27}, target output: ${targetOutput27}`,
255260
);
256261

257262
const currentOutput28 = formatAs12HourClock("15:-30");
258263
const targetOutput28 = "Not a valid time";
259264
console.assert(
260265
currentOutput28 === targetOutput28,
261-
`current output: ${currentOutput28}, target output: ${targetOutput28}`
266+
`current output: ${currentOutput28}, target output: ${targetOutput28}`,
262267
);
263268

264269
const currentOutput29 = formatAs12HourClock("-15:30");
265270
const targetOutput29 = "Not a valid time";
266271
console.assert(
267272
currentOutput29 === targetOutput29,
268-
`current output: ${currentOutput29}, target output: ${targetOutput29}`
273+
`current output: ${currentOutput29}, target output: ${targetOutput29}`,
269274
);
270-
271-

0 commit comments

Comments
 (0)