Skip to content

Commit 0f98c63

Browse files
committed
done format-time.js
1 parent f77a0be commit 0f98c63

File tree

1 file changed

+98
-11
lines changed

1 file changed

+98
-11
lines changed

Sprint-2/extend/format-time.js

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,111 @@
1-
// This is the latest solution to the problem from the prep.
2-
// 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+
/*
2+
This is the latest solution to the problem from the prep.
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.
4+
5+
function formatAs12HourClock(time) {
6+
const hours = Number(time.slice(0, 2));
7+
console.log(hours);
8+
if (hours > 12) {
9+
return `${hours - 12}:00 pm`;
10+
}
11+
return `${time} am`;
12+
}
13+
14+
*/
15+
16+
// ====================== function and cases =======================
17+
18+
19+
// time.slice(0, 2): method that return the positions 0 and 1 of the string,
20+
// const hours = Number(time.slice(0, 2)) will convert the substring in numbes to manipulate and perfoms calculations.
21+
322

423
function formatAs12HourClock(time) {
5-
const hours = Number(time.slice(0, 2));
6-
if (hours > 12) {
7-
return `${hours - 12}:00 pm`;
24+
const hours = Number(time.slice(0, 2)); //Extracts characters from position 0 to 2 (not including the character at index 2)
25+
const minutes = time.slice(3, 5); // extract characters from position 3 to 5 (for minutes part)
26+
27+
if (hours === 0) {
28+
return `12:${minutes} am`;
29+
} else if (hours === 12) {
30+
return `12:${minutes} pm`;
31+
} else if (hours > 12) {
32+
return `${hours - 12}:${minutes} pm`;
33+
} else {
34+
return `${hours}:${minutes} am`;
835
}
9-
return `${time} am`;
36+
//the previous conditions always lead to a return
1037
}
1138

12-
const currentOutput = formatAs12HourClock("08:00");
13-
const targetOutput = "08:00 am";
39+
40+
// ============================= test ===============================
41+
42+
const currentOutput = formatAs12HourClock("00:42");
43+
const targetOutput = "12:42 am";
1444
console.assert(
1545
currentOutput === targetOutput,
1646
`current output: ${currentOutput}, target output: ${targetOutput}`
1747
);
1848

19-
const currentOutput2 = formatAs12HourClock("23:00");
20-
const targetOutput2 = "11:00 pm";
49+
50+
// ============================= test 1 =============================
51+
52+
const currentOutput1 = formatAs12HourClock("00:00");
53+
const targetOutput1 = "12:00 am";
54+
console.assert(
55+
currentOutput1 === targetOutput1,
56+
`current output: ${currentOutput1}, target output: ${targetOutput1}`
57+
);
58+
59+
60+
// ============================= test 2 failed ======================
61+
62+
const currentOutput2 = formatAs12HourClock("26:00"); //26 > 14; go in the second else if, 26-12=14
63+
const targetOutput2 = "10:00 pm";
64+
2165
console.assert(
2266
currentOutput2 === targetOutput2,
23-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
67+
`current output time: ${currentOutput2}, target output: ${targetOutput2}`
68+
//will print Assertion failed: current output time: 14:00 pm, target output: 10:00 pm
2469
);
70+
71+
72+
73+
const testCases = [
74+
{ input: "00:00", expected: "12:00 am" },
75+
{ input: "12:00", expected: "12:00 pm" },
76+
{ input: "15:30", expected: "3:30 pm" },
77+
{ input: "08:05", expected: "8:05 am" },
78+
{ input: "23:59", expected: "11:59 pm" },
79+
];
80+
81+
for (const { input, expected } of testCases) {
82+
const result = formatAs12HourClock(input);
83+
console.assert(
84+
result === expected,
85+
`Input: ${input} | Expected: ${expected}, but got: ${result}`
86+
);
87+
}
88+
89+
// ================ icons test with console/log ========================
90+
91+
const testCasesIcons = [
92+
{ input: "25:00", expected: "12:00 am" }, //test failed
93+
{ input: "12:00", expected: "12:00 pm" },
94+
{ input: "15:30", expected: "3:30 pm" },
95+
{ input: "08:05", expected: "8:05 am" },
96+
{ input: "23:59", expected: "11:59 pm" },
97+
];
98+
99+
100+
for (const { input, expected } of testCasesIcons) {
101+
const result = formatAs12HourClock(input);
102+
if (result === expected) {
103+
console.log(`✅ Test passed! Input: ${input} | Output: ${result}`);
104+
} else {
105+
console.error(
106+
`❌ Test failed! Input: ${input} | Expected: ${expected}, but got: ${result}`
107+
);
108+
}
109+
}
110+
111+

0 commit comments

Comments
 (0)