Skip to content

Commit d512feb

Browse files
committed
format-time.js committed
1 parent 90cb423 commit d512feb

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,114 @@ console.assert(
2323
currentOutput2 === targetOutput2,
2424
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2525
);
26+
27+
// Test suite for formatAs12HourClock function
28+
29+
function runTests() {
30+
const testCases = [
31+
// Edge cases and boundaries
32+
{ input: "00:00", expected: "12:00 am", description: "Midnight" },
33+
{ input: "12:00", expected: "12:00 pm", description: "Noon" },
34+
{ input: "00:01", expected: "12:01 am", description: "Just after midnight" },
35+
{ input: "11:59", expected: "11:59 am", description: "Just before noon" },
36+
{ input: "12:01", expected: "12:01 pm", description: "Just after noon" },
37+
{ input: "23:59", expected: "11:59 pm", description: "Just before midnight" },
38+
39+
// AM hours
40+
{ input: "01:00", expected: "01:00 am", description: "1 AM" },
41+
{ input: "02:30", expected: "02:30 am", description: "2:30 AM" },
42+
{ input: "09:15", expected: "09:15 am", description: "9:15 AM" },
43+
{ input: "11:45", expected: "11:45 am", description: "11:45 AM" },
44+
45+
// PM hours
46+
{ input: "13:00", expected: "01:00 pm", description: "1 PM" },
47+
{ input: "14:30", expected: "02:30 pm", description: "2:30 PM" },
48+
{ input: "17:45", expected: "05:45 pm", description: "5:45 PM" },
49+
{ input: "22:15", expected: "10:15 pm", description: "10:15 PM" },
50+
51+
// Invalid inputs
52+
{ input: "24:00", expected: "12:00 am", description: "24:00 (should be midnight)" },
53+
{ input: "25:00", expected: null, description: "Invalid hour > 24" },
54+
{ input: "12:60", expected: null, description: "Invalid minutes > 59" },
55+
{ input: "9:00", expected: null, description: "Missing leading zero" },
56+
{ input: "09-00", expected: null, description: "Wrong separator" },
57+
{ input: "abc", expected: null, description: "Non-numeric input" },
58+
{ input: "", expected: null, description: "Empty string" },
59+
{ input: "12", expected: null, description: "Incomplete time" },
60+
{ input: "12345", expected: null, description: "Too many digits" },
61+
];
62+
63+
console.log("Running tests for formatAs12HourClock...\n");
64+
65+
let passed = 0;
66+
let failed = 0;
67+
68+
testCases.forEach((testCase, index) => {
69+
try {
70+
const result = formatAs12HourClock(testCase.input);
71+
const passed_test = result === testCase.expected;
72+
73+
if (passed_test) {
74+
console.log(`Test ${index + 1}: ${testCase.description} - PASSED`);
75+
passed++;
76+
} else {
77+
console.log(`Test ${index + 1}: ${testCase.description} - FAILED`);
78+
console.log(`Input: "${testCase.input}"`);
79+
console.log(`Expected: "${testCase.expected}"`);
80+
console.log(`Got: "${result}"\n`);
81+
failed++;
82+
}
83+
} catch (error) {
84+
console.log(`Test ${index + 1}: ${testCase.description} - ERROR`);
85+
console.log(`Input: "${testCase.input}"`);
86+
console.log(`Error: ${error.message}\n`);
87+
failed++;
88+
}
89+
});
90+
91+
console.log(`\nTests completed: ${passed} passed, ${failed} failed`);
92+
}
93+
94+
// Run the tests
95+
runTests();
96+
97+
// Fixed Code:
98+
99+
function formatAs12HourClock(time) {
100+
// Input validation
101+
if (typeof time !== 'string' || !time) {
102+
return null;
103+
}
104+
105+
// Check format: HH:MM with leading zeros
106+
const timeRegex = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/;
107+
if (!timeRegex.test(time)) {
108+
return null;
109+
}
110+
111+
const hours = Number(time.slice(0, 2));
112+
const minutes = time.slice(3, 5);
113+
114+
// Handle midnight (00:00)
115+
if (hours === 0) {
116+
return `12:${minutes} am`;
117+
}
118+
119+
// Handle noon (12:00) and other PM hours
120+
if (hours === 12) {
121+
return `12:${minutes} pm`;
122+
}
123+
124+
if (hours > 12) {
125+
const pmHours = hours - 12;
126+
// Add leading zero for hours 1-9
127+
const formattedHours = pmHours < 10 ? `0${pmHours}` : `${pmHours}`;
128+
return `${formattedHours}:${minutes} pm`;
129+
}
130+
131+
// AM hours (1-11)
132+
// Keep leading zero for hours 1-9
133+
const formattedHours = hours < 10 ? `0${hours}` : `${hours}`;
134+
return `${formattedHours}:${minutes} am`;
135+
}
136+

0 commit comments

Comments
 (0)