Skip to content

Commit 0ba4f94

Browse files
committed
sprint 2 completed
1 parent d93eb5d commit 0ba4f94

File tree

1 file changed

+70
-10
lines changed

1 file changed

+70
-10
lines changed

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,84 @@
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.
44

5+
/*
6+
edge cases to be checked:
7+
1. 00:00
8+
2. 24:00
9+
3. 12:00
10+
4. 12:01
11+
5. 00:01
12+
*/
13+
14+
function pad(num) {
15+
return num.toString().padStart(2, "0");
16+
}
17+
518
function formatAs12HourClock(time) {
619
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
9-
}
10-
return `${time} am`;
20+
const minutes = Number(time.slice(3, 5));
21+
22+
if (hours == 0 || hours == 24) {
23+
return `12:${pad(minutes)} am`;
24+
} else if (hours > 12) {
25+
return `${pad(hours - 12)}:00 pm`;
26+
} else if (hours == 12) {
27+
return `${time} pm`;
28+
} else return `${time} am`;
1129
}
1230

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
31+
let currentOutput = formatAs12HourClock("08:00");
32+
let targetOutput = "08:00 am";
33+
console.assert(
34+
currentOutput === targetOutput,
35+
`current output: ${currentOutput}, target output: ${targetOutput}`
36+
);
37+
38+
currentOutput = formatAs12HourClock("23:00");
39+
targetOutput = "11:00 pm";
40+
console.assert(
41+
currentOutput === targetOutput,
42+
`current output: ${currentOutput}, target output: ${targetOutput}`
43+
);
44+
45+
currentOutput = formatAs12HourClock("13:00");
46+
targetOutput = "01:00 pm";
47+
console.assert(
48+
currentOutput === targetOutput,
49+
`current output: ${currentOutput}, target output: ${targetOutput}`
50+
);
51+
52+
currentOutput = formatAs12HourClock("00:00");
53+
targetOutput = "12:00 am";
54+
console.assert(
55+
currentOutput === targetOutput,
56+
`current output: ${currentOutput}, target output: ${targetOutput}`
57+
);
58+
59+
currentOutput = formatAs12HourClock("24:00");
60+
targetOutput = "12:00 am";
61+
console.assert(
62+
currentOutput === targetOutput,
63+
`current output: ${currentOutput}, target output: ${targetOutput}`
64+
);
65+
66+
currentOutput = formatAs12HourClock("12:00");
67+
targetOutput = "12:00 pm";
1568
console.assert(
1669
currentOutput === targetOutput,
1770
`current output: ${currentOutput}, target output: ${targetOutput}`
1871
);
1972

20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
73+
currentOutput = formatAs12HourClock("12:01");
74+
targetOutput = "12:01 pm";
2275
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
76+
currentOutput === targetOutput,
77+
`current output: ${currentOutput}, target output: ${targetOutput}`
78+
);
79+
80+
currentOutput = formatAs12HourClock("00:01");
81+
targetOutput = "12:01 am";
82+
console.assert(
83+
currentOutput === targetOutput,
84+
`current output: ${currentOutput}, target output: ${targetOutput}`
2585
);

0 commit comments

Comments
 (0)