Skip to content

Commit ff49d6c

Browse files
committed
Fixed time format function bugs, and run test cases
1 parent 87fc640 commit ff49d6c

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,49 @@
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

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
6+
const hours = time.slice(0, 2);
7+
const minutes = time.slice(3, 5);
8+
79
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
10+
return `${Number(hours) - 12}:${minutes} pm`;
11+
} else if (Number(hours) === 0) {
12+
return `12:${minutes} am`;
913
}
10-
return `${time} am`;
14+
return `${hours}:${minutes} am`;
1115
}
1216

1317
//Test cases
1418
const currentOutput = formatAs12HourClock("08:00");
1519
const targetOutput = "08:00 am";
16-
console.assert(currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}`);
20+
console.assert(
21+
currentOutput === targetOutput,
22+
`current output: ${currentOutput}, target output: ${targetOutput}`
23+
);
1724

1825
const currentOutput2 = formatAs12HourClock("23:00");
1926
const targetOutput2 = "11:00 pm";
20-
console.assert(currentOutput2 === targetOutput2,`current output: ${currentOutput2}, target output: ${targetOutput2}`);
21-
27+
console.assert(
28+
currentOutput2 === targetOutput2,
29+
`current output: ${currentOutput2}, target output: ${targetOutput2}`
30+
);
2231

2332
const currentOutput3 = formatAs12HourClock("00:00");
2433
const targetOutput3 = "12:00 am";
25-
console.assert(currentOutput3 === targetOutput3,`current output: ${currentOutput3}, target output: ${targetOutput3}`);
26-
27-
28-
const currentOutput4= formatAs12HourClock("00:00");
29-
const targetOutput4= "12:00 am";
30-
console.assert(currentOutput4 === targetOutput4,`current output: ${currentOutput4}, target output: ${targetOutput4}`);
31-
32-
const currentOutput5= formatAs12HourClock("15:01");
33-
const targetOutput5= "3:01 pm";
34-
console.assert(currentOutput5 === targetOutput5,`current output: ${currentOutput5}, target output: ${targetOutput5}`);
34+
console.assert(
35+
currentOutput3 === targetOutput3,
36+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
37+
);
38+
39+
const currentOutput5 = formatAs12HourClock("15:01");
40+
const targetOutput5 = "3:01 pm";
41+
console.assert(
42+
currentOutput5 === targetOutput5,
43+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
44+
);
45+
46+
const currentOutput6 = formatAs12HourClock("15:01");
47+
const targetOutput6 = "3:01 pm";
48+
console.assert(
49+
currentOutput6 === targetOutput6,
50+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
51+
);

0 commit comments

Comments
 (0)