Skip to content

Commit c5d6da1

Browse files
committed
Strech-extend tasks are done
1 parent 77a80c6 commit c5d6da1

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed
Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,85 @@
1-
// This is the latest solution to the problem from the prep.
2-
// Make sure to do the prep before you do the coursework
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.
1+
// // This is the latest solution to the problem from the prep.
2+
// // Make sure to do the prep before you do the coursework
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.
44

5+
// function formatAs12HourClock(time) {
6+
// const hours = Number(time.slice(0, 2));
7+
// if (hours > 12) {
8+
// return `${hours - 12}:00 pm`;
9+
// }
10+
// return `${time} am`;
11+
// }
12+
13+
// const currentOutput = formatAs12HourClock("08:00");
14+
// const targetOutput = "08:00 am";
15+
// console.assert(
16+
// currentOutput === targetOutput,
17+
// `current output: ${currentOutput}, target output: ${targetOutput}`
18+
// );
19+
20+
// const currentOutput2 = formatAs12HourClock("23:00");
21+
// const targetOutput2 = "11:00 pm";
22+
// console.assert(
23+
// currentOutput2 === targetOutput2,
24+
// `current output: ${currentOutput2}, target output: ${targetOutput2}`
25+
// );
26+
27+
// // ======= TESTS =======
28+
29+
// // Before midday
30+
// console.log(formatAs12HourClock("08:00")); // 08:00 am
31+
// console.log(formatAs12HourClock("09:45")); // 09:45 am
32+
33+
// // Midday / Noon
34+
// console.log(formatAs12HourClock("12:00")); // 12:00 pm
35+
// console.log(formatAs12HourClock("12:30")); // 12:30 pm
36+
37+
// // After midday
38+
// console.log(formatAs12HourClock("14:15")); // 02:15 pm
39+
// console.log(formatAs12HourClock("23:00")); // 11:00 pm
40+
41+
// // Midnight
42+
// console.log(formatAs12HourClock("00:00")); // 12:00 am
43+
// console.log(formatAs12HourClock("00:30")); // 12:30 am
44+
45+
// Function to convert 24-hour time to 12-hour time
546
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
7-
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
47+
// Split the time into hours and minutes
48+
let parts = time.split(":");
49+
let hours = Number(parts[0]);
50+
let minutes = parts[1];
51+
52+
// Decide if it's am or pm
53+
let suffix = hours >= 12 ? "pm" : "am";
54+
55+
// Convert hours to 12-hour format
56+
if (hours === 0) {
57+
hours = 12; // midnight
58+
} else if (hours > 12) {
59+
hours = hours - 12; // afternoon
960
}
10-
return `${time} am`;
61+
62+
// Make sure hours are always 2 digits
63+
let hoursStr = hours.toString().padStart(2, "0");
64+
65+
// Return the formatted time
66+
return hoursStr + ":" + minutes + " " + suffix;
1167
}
1268

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
19-
20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22-
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);
69+
// ======= TESTS =======
70+
71+
// Before midday
72+
console.log(formatAs12HourClock("08:00"));
73+
console.log(formatAs12HourClock("09:45"));
74+
75+
// Midday / Noon
76+
console.log(formatAs12HourClock("12:00"));
77+
console.log(formatAs12HourClock("12:30"));
78+
79+
// After midday
80+
console.log(formatAs12HourClock("14:15"));
81+
console.log(formatAs12HourClock("23:00"));
82+
83+
// Midnight
84+
console.log(formatAs12HourClock("00:00"));
85+
console.log(formatAs12HourClock("00:30"));

0 commit comments

Comments
 (0)