2
2
// Make sure to do the prep before you do the coursework
3
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
4
5
+ // function formatAs12HourClock(time) {
6
+ // const hours = Number(time.slice(0, 2));
7
+ // if (hours > 12) {
8
+ // return `${hours - 12}:00 pm`;
9
+ // }
10
+ // return `${time} am`;
11
+ // }
12
+
13
+ // const currentOutput = formatAs12HourClock("08:00");
14
+ // const targetOutput = "08:00 am";
15
+ // console.assert(
16
+ // currentOutput === targetOutput,
17
+ // `current output: ${currentOutput}, target output: ${targetOutput}`
18
+ // );
19
+
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
+ // );
26
+
27
+
28
+
5
29
function formatAs12HourClock ( time ) {
6
30
const hours = Number ( time . slice ( 0 , 2 ) ) ;
31
+ const minutes = time . slice ( 3 , 5 ) ;
32
+
33
+ if ( hours === 0 ) {
34
+ return `12:${ minutes } am` ;
35
+ }
36
+ if ( hours === 12 ) {
37
+ return `12:${ minutes } pm` ;
38
+ }
7
39
if ( hours > 12 ) {
8
- return `${ hours - 12 } :00 pm` ;
40
+ return `${ pad ( hours - 12 ) } : ${ minutes } pm` ;
9
41
}
10
- return `${ time } am` ;
42
+ return `${ pad ( hours ) } :${ minutes } am` ;
43
+ }
44
+
45
+ function pad ( num ) {
46
+ return num . toString ( ) . padStart ( 2 , "0" ) ;
11
47
}
12
48
13
- const currentOutput = formatAs12HourClock ( "08:00" ) ;
14
- const targetOutput = "08:00 am" ;
15
- console . assert (
16
- currentOutput === targetOutput ,
17
- `current output: ${ currentOutput } , target output: ${ targetOutput } `
18
- ) ;
19
-
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
- ) ;
49
+ // Test cases for different groups of input data and edge cases
50
+ console . assert ( formatAs12HourClock ( "00:00" ) === "12:00 am" , "Test midnight failed" ) ;
51
+ console . assert ( formatAs12HourClock ( "01:00" ) === "01:00 am" , "Test 1am failed" ) ;
52
+ console . assert ( formatAs12HourClock ( "08:00" ) === "08:00 am" , "Test 8am failed" ) ;
53
+ console . assert ( formatAs12HourClock ( "12:00" ) === "12:00 pm" , "Test noon failed" ) ;
54
+ console . assert ( formatAs12HourClock ( "13:00" ) === "01:00 pm" , "Test 1pm failed" ) ;
55
+ console . assert ( formatAs12HourClock ( "11:59" ) === "11:59 am" , "Test 11:59am failed" ) ;
56
+ console . assert ( formatAs12HourClock ( "12:59" ) === "12:59 pm" , "Test 12:59pm failed" ) ;
57
+ console . assert ( formatAs12HourClock ( "15:30" ) === "03:30 pm" , "Test 3:30pm failed" ) ;
58
+ console . assert ( formatAs12HourClock ( "23:00" ) === "11:00 pm" , "Test 11pm failed" ) ;
59
+ console . assert ( formatAs12HourClock ( "23:59" ) === "11:59 pm" , "Test 11:59pm failed" ) ;
0 commit comments