Skip to content

Commit aaefd95

Browse files
committed
format-time MSTD_Sprint2 function to convert 24 hour clock times to 12 hour clock times, solved and tested.
1 parent 7dc2179 commit aaefd95

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,39 @@
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+
if (!time.includes(':')){
7+
time = time.slice(0,-2)+':' + time.slice(-2)
8+
}
69
const hours = Number(time.slice(0, 2));
710
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
11+
const minutes = time.slice(3);
12+
return `${hours - 12}:${minutes} pm`;
13+
}else if (time.length < 5){
14+
time = time.padStart(5,'0')
915
}
1016
return `${time} am`;
1117
}
1218

1319
const currentOutput = formatAs12HourClock("08:00");
1420
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
21+
console.assert(currentOutput === targetOutput,`current output1: ${currentOutput}, target output: ${targetOutput}`);
22+
23+
const currentOutput2 = formatAs12HourClock("23:01");
24+
const targetOutput2 = "11:01 pm";
25+
console.assert(currentOutput2 === targetOutput2,`current output2: ${currentOutput2}, target output: ${targetOutput2}`);
26+
27+
const currentOutput3 = formatAs12HourClock("07:34");
28+
const targetOutput3 = "07:34 am";
29+
console.assert(currentOutput3 === targetOutput3,`current output3: ${currentOutput3}, target output: ${targetOutput3}`);
30+
31+
const currentOutput4 = formatAs12HourClock("1100");
32+
const targetOutput4 = ("11:00 am");
33+
console.assert(currentOutput4 === targetOutput4,`current output4: ${currentOutput4}, target output: ${targetOutput4}`);
34+
35+
const currentOutput5 = formatAs12HourClock("8:24");
36+
const targetOutput5 = "08:24 am";
37+
console.assert(currentOutput5 === targetOutput5,`current output5: ${currentOutput5}, target output: ${targetOutput5}`);
1938

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-
);
39+
const currentOutput6 = formatAs12HourClock("1:01");
40+
const targetOutput6 = "01:01 am";
41+
console.assert(currentOutput6 === targetOutput6,`current output6: ${currentOutput6}, target output: ${targetOutput6}`);

0 commit comments

Comments
 (0)