Skip to content

Commit 38825cb

Browse files
committed
Fix bugs, extend code to catch different inputs. Write tests
1 parent f54452f commit 38825cb

1 file changed

Lines changed: 250 additions & 4 deletions

File tree

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

Lines changed: 250 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,66 @@
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-
4+
function pad(num){
5+
let stringNum = num.toString();
6+
if (stringNum.length < 2){
7+
stringNum = "0" + stringNum;
8+
return stringNum;
9+
}
10+
else{
11+
return stringNum;
12+
}
13+
}
514
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
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 .
17+
}
18+
19+
if(time.indexOf(":") === -1){
20+
if(time.indexOf(".") !== time.lastIndexOf("."))
21+
{
22+
return "Not a valid time" //check there is only one .
23+
}
24+
}
25+
if(time.indexOf(".") === -1){
26+
if(time.indexOf(":") !== time.lastIndexOf(":"))
27+
{
28+
return "Not a valid time" //check if there is only one :
29+
}
30+
31+
}
32+
33+
let colonPeriodIndex = 0;
34+
if(time.indexOf(":") === -1){
35+
colonPeriodIndex = time.indexOf(".")
36+
}
37+
else
38+
{
39+
colonPeriodIndex = time.indexOf(":")
40+
}
41+
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
44+
}
45+
const hours = Number(time.slice(0, colonPeriodIndex));
46+
const minutes = Number(time.slice(colonPeriodIndex+1));
47+
const stringMinutes = pad(minutes);
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){
54+
return `${stringHours}:${stringMinutes} pm`;
55+
}
56+
if (hours === 0){
57+
return `12:${stringMinutes} am`;
58+
}
759
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
60+
return `${pad(hours - 12)}:${stringMinutes} pm`;
961
}
10-
return `${time} am`;
62+
63+
return `${stringHours}:${stringMinutes} am`;
1164
}
1265

1366
const currentOutput = formatAs12HourClock("08:00");
@@ -23,3 +76,196 @@ console.assert(
2376
currentOutput2 === targetOutput2,
2477
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2578
);
79+
80+
const currentOutput3 = formatAs12HourClock("23:46");
81+
const targetOutput3 = "11:46 pm";
82+
console.assert(
83+
currentOutput3 === targetOutput3,
84+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
85+
);
86+
87+
const currentOutput4 = formatAs12HourClock("12:00");
88+
const targetOutput4 = "12:00 pm";
89+
console.assert(
90+
currentOutput4 === targetOutput4,
91+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
92+
);
93+
94+
const currentOutput5 = formatAs12HourClock("00:00");
95+
const targetOutput5 = "12:00 am";
96+
console.assert(
97+
currentOutput5 === targetOutput5,
98+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
99+
);
100+
101+
const currentOutput6 = formatAs12HourClock("24:00");
102+
const targetOutput6 = "Not a valid time";
103+
console.assert(
104+
currentOutput6 === targetOutput6,
105+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
106+
);
107+
108+
const currentOutput7 = formatAs12HourClock("47:00");
109+
const targetOutput7 = "Not a valid time";
110+
console.assert(
111+
currentOutput7 === targetOutput7,
112+
`current output: ${currentOutput7}, target output: ${targetOutput7}`
113+
);
114+
115+
const currentOutput8 = formatAs12HourClock("15:75");
116+
const targetOutput8 = "Not a valid time";
117+
console.assert(
118+
currentOutput8 === targetOutput8,
119+
`current output: ${currentOutput8}, target output: ${targetOutput8}`
120+
);
121+
122+
const currentOutput9 = formatAs12HourClock("18:60");
123+
const targetOutput9 = "Not a valid time";
124+
console.assert(
125+
currentOutput9 === targetOutput9,
126+
`current output: ${currentOutput9}, target output: ${targetOutput9}`
127+
);
128+
129+
const currentOutput10 = formatAs12HourClock("23:59");
130+
const targetOutput10 = "11:59 pm";
131+
console.assert(
132+
currentOutput10 === targetOutput10,
133+
`current output: ${currentOutput10}, target output: ${targetOutput10}`
134+
);
135+
136+
const currentOutput11 = formatAs12HourClock("00:01");
137+
const targetOutput11 = "12:01 am";
138+
console.assert(
139+
currentOutput11 === targetOutput11,
140+
`current output: ${currentOutput11}, target output: ${targetOutput11}`
141+
);
142+
143+
const currentOutput12 = formatAs12HourClock("12:01");
144+
const targetOutput12 = "12:01 pm";
145+
console.assert(
146+
currentOutput12 === targetOutput12,
147+
`current output: ${currentOutput12}, target output: ${targetOutput12}`
148+
);
149+
150+
const currentOutput13 = formatAs12HourClock("13:00");
151+
const targetOutput13 = "01:00 pm";
152+
console.assert(
153+
currentOutput13 === targetOutput13,
154+
`current output: ${currentOutput13}, target output: ${targetOutput13}`
155+
);
156+
157+
const currentOutput14 = formatAs12HourClock("hello");
158+
const targetOutput14 = "Not a valid time";
159+
console.assert(
160+
currentOutput14 === targetOutput14,
161+
`current output: ${currentOutput14}, target output: ${targetOutput14}`
162+
);
163+
164+
const currentOutput15 = formatAs12HourClock("15:ab");
165+
const targetOutput15 = "Not a valid time";
166+
console.assert(
167+
currentOutput15 === targetOutput15,
168+
`current output: ${currentOutput15}, target output: ${targetOutput15}`
169+
);
170+
171+
172+
const currentOutput16 = formatAs12HourClock("12");
173+
const targetOutput16 = "Not a valid time";
174+
console.assert(
175+
currentOutput16 === targetOutput16,
176+
`current output: ${currentOutput16}, target output: ${targetOutput16}`
177+
);
178+
179+
const currentOutput17 = formatAs12HourClock("12:30abc");
180+
const targetOutput17 = "Not a valid time";
181+
console.assert(
182+
currentOutput17 === targetOutput17,
183+
`current output: ${currentOutput17}, target output: ${targetOutput17}`
184+
);
185+
186+
const currentOutput18 = formatAs12HourClock("1.30");
187+
const targetOutput18 = "01:30 am";
188+
console.assert(
189+
currentOutput18 === targetOutput18,
190+
`current output: ${currentOutput18}, target output: ${targetOutput18}`
191+
);
192+
193+
const currentOutput19 = formatAs12HourClock("15.30");
194+
const targetOutput19 = "03:30 pm";
195+
console.assert(
196+
currentOutput19 === targetOutput19,
197+
`current output: ${currentOutput19}, target output: ${targetOutput19}`
198+
);
199+
200+
const currentOutput20 = formatAs12HourClock("01.30");
201+
const targetOutput20 = "01:30 am";
202+
console.assert(
203+
currentOutput20 === targetOutput20,
204+
`current output: ${currentOutput20}, target output: ${targetOutput20}`
205+
);
206+
207+
const currentOutput21 = formatAs12HourClock("12.30:45");
208+
const targetOutput21 = "Not a valid time";
209+
console.assert(
210+
currentOutput21 === targetOutput21,
211+
`current output: ${currentOutput21}, target output: ${targetOutput21}`
212+
);
213+
214+
215+
const currentOutput22 = formatAs12HourClock("12::30");
216+
const targetOutput22 = "Not a valid time";
217+
console.assert(
218+
currentOutput22 === targetOutput22,
219+
`current output: ${currentOutput22}, target output: ${targetOutput22}`
220+
);
221+
222+
const currentOutput23 = formatAs12HourClock("12..30");
223+
const targetOutput23 = "Not a valid time";
224+
console.assert(
225+
currentOutput23 === targetOutput23,
226+
`current output: ${currentOutput23}, target output: ${targetOutput23}`
227+
);
228+
229+
const currentOutput24 = formatAs12HourClock("12:");
230+
const targetOutput24 = "Not a valid time";
231+
console.assert(
232+
currentOutput24 === targetOutput24,
233+
`current output: ${currentOutput24}, target output: ${targetOutput24}`
234+
);
235+
236+
const currentOutput25 = formatAs12HourClock("12.");
237+
const targetOutput25 = "Not a valid time";
238+
console.assert(
239+
currentOutput25 === targetOutput25,
240+
`current output: ${currentOutput25}, target output: ${targetOutput25}`
241+
);
242+
243+
const currentOutput26 = formatAs12HourClock(":30");
244+
const targetOutput26 = "Not a valid time";
245+
console.assert(
246+
currentOutput26 === targetOutput26,
247+
`current output: ${currentOutput26}, target output: ${targetOutput26}`
248+
);
249+
250+
const currentOutput27 = formatAs12HourClock(".");
251+
const targetOutput27 = "Not a valid time";
252+
console.assert(
253+
currentOutput27 === targetOutput27,
254+
`current output: ${currentOutput27}, target output: ${targetOutput27}`
255+
);
256+
257+
const currentOutput28 = formatAs12HourClock("15:-30");
258+
const targetOutput28 = "Not a valid time";
259+
console.assert(
260+
currentOutput28 === targetOutput28,
261+
`current output: ${currentOutput28}, target output: ${targetOutput28}`
262+
);
263+
264+
const currentOutput29 = formatAs12HourClock("-15:30");
265+
const targetOutput29 = "Not a valid time";
266+
console.assert(
267+
currentOutput29 === targetOutput29,
268+
`current output: ${currentOutput29}, target output: ${targetOutput29}`
269+
);
270+
271+

0 commit comments

Comments
 (0)