Skip to content

Commit 6fedd2a

Browse files
authored
Refactor time formatting and update test cases
Refactor formatAs12HourClock function to improve readability and fix output format. Adjust test cases to match expected output format without spaces.
1 parent c56be30 commit 6fedd2a

1 file changed

Lines changed: 38 additions & 16 deletions

File tree

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,70 @@
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any
44
// bugs you find.
55

6+
// This is the latest solution to the problem from the prep.
7+
// Make sure to do the prep before you do the coursework
8+
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any
9+
// bugs you find.
10+
611
function formatAs12HourClock(time) {
7-
if (Number(time.slice(0, 2)) === 12) {
8-
return `${time} pm` // if time is "12:00" it will show
12+
const hours = Number(time.slice(0, 2)); // "hours" variable represents first two digits of the time string turned into number
13+
const minutes = Number(time.slice(-2));// "mninutes" variable represents last two digits of the "time" string converted to number
14+
if (hours === 12) {
15+
return `${time}pm` // if time is "12:00" it will show
916
//12:00 pm
10-
} else if (Number(time.slice(0, 2))=== 24) {
11-
return `00:00 am`;
12-
} else if (Number(time.slice(0, 2)) > 12) {
13-
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
17+
} else if (hours === 24 && time.slice(-2) === "00") {
18+
return `00:00am`;
19+
} else if (hours === 24 && minutes > 0) {
20+
return `00:${minutes}am`
21+
22+
}else if (hours > 12) {
23+
return `${hours - 12}:${minutes}pm`;
1424
}
15-
return `${time} am`;
25+
return `${time}am`;
1626
}
1727
const currentOutput = formatAs12HourClock("08:00");
18-
const targetOutput = "08:00 am";
28+
const targetOutput = "08:00am";
1929
console.assert(
2030
currentOutput === targetOutput,
2131
`current output: ${currentOutput}, target output: ${targetOutput}`
2232
);
2333

24-
const currentOutput2 = formatAs12HourClock("23:00");
25-
const targetOutput2 = "11:00 pm";
34+
const currentOutput2 = formatAs12HourClock("23:35");
35+
const targetOutput2 = "11:35pm";
2636
console.assert(
2737
currentOutput2 === targetOutput2,
28-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
38+
`current output2: ${currentOutput2}, target output2: ${targetOutput2}`
2939
);
3040
const currentOutput3 = formatAs12HourClock("00:00");//
31-
const targetOutput3 = "00:00 am";// expecting new test to return "00:00 am"
41+
const targetOutput3 = "00:00am";// expecting new test to return "00:00 am"
3242
console.assert(
3343
currentOutput3 === targetOutput3,
34-
`current output: ${currentOutput3}, target output: ${targetOutput3}`
44+
`current output3: ${currentOutput3}, target output3: ${targetOutput3}`
3545
); // if currentOutput3 and targetOutput3 do not match, the assertion message will be given.
3646

3747
const currentOutput4 = formatAs12HourClock("12:00");//
38-
const targetOutput4 = "12:00 pm"; // expected output for "12:00" is "12:00 pm"
48+
const targetOutput4 = "12:00pm"; // expected output for "12:00" is "12:00 pm"
3949
console.assert(
4050
currentOutput4 === targetOutput4,
41-
`current output: ${currentOutput4}, target output: ${targetOutput4}`
51+
`current output4: ${currentOutput4}, target output4: ${targetOutput4}`
4252
);// if currentOutput and targetOutput do not match, the assertion message will be given.
4353
const currentOutput5 = formatAs12HourClock("24:00");
44-
const targetOutput5 = "00:00 am";
54+
const targetOutput5 = "00:00am";
4555
console.assert(
4656
currentOutput5 === targetOutput5,
4757
`current output5: ${currentOutput5}, target output5: ${targetOutput5}`
4858
); // if 24:00 is entered and the output is not "00:00 am" the assertion message will be given
4959

60+
// below is test to try with minutes
61+
const currentOutput6 = formatAs12HourClock("22:55");
62+
const targetOutput6 = "10:55pm";
63+
console.assert(currentOutput6 === targetOutput6, `current outpu6: ${currentOutput6}, target output6: ${targetOutput6}`);
64+
65+
// following test tests what happens if time passed 24:00 hours.
66+
const currentOutput7 = formatAs12HourClock("24:25");
67+
const targetOutput7 = "00:25am";
68+
console.assert(currentOutput7 === targetOutput7, `current output7: ${currentOutput7}, target output: ${targetOutput7}`)
69+
70+
71+
5072

0 commit comments

Comments
 (0)