1+
12// This is the latest solution to the problem from the prep.
23// Make sure to do the prep before you do the coursework
34// 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.
@@ -23,3 +24,57 @@ console.assert(
2324 currentOutput2 === targetOutput2 ,
2425 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2526) ;
27+
28+ const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
29+ const targetOutput3 = "12:00 am" ;
30+ console . assert (
31+ currentOutput3 === targetOutput3 ,
32+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
33+ ) ;
34+
35+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
36+ const targetOutput4 = "12:00 pm" ;
37+ console . assert (
38+ currentOutput4 === targetOutput4 ,
39+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
40+ ) ;
41+
42+ const currentOutput5 = formatAs12HourClock ( "13:00" ) ;
43+ const targetOutput5 = "1:00 pm" ;
44+ console . assert (
45+ currentOutput5 === targetOutput5 ,
46+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
47+ ) ;
48+
49+ const currentOutput7 = formatAs12HourClock ( "25:00" ) ;
50+ const targetOutput7 = "01:00 am" ;
51+ console . assert (
52+ currentOutput7 === targetOutput7 ,
53+ `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
54+ ) ;
55+
56+ // modified code:
57+
58+ function formatAs12HourClock ( time ) {
59+ let hours = Number ( time . slice ( 0 , 2 ) ) ;
60+ const minutes = time . slice ( 3 ) ;
61+
62+ let suffix ;
63+ if ( hours >= 12 ) {
64+ suffix = "pm" ;
65+
66+ } else {
67+ suffix = "am" ;
68+ }
69+ hours = hours % 12 || 12 ; // Convert 0 to 12, 13 to 1
70+ const formattedHours = hours . toString ( ) . padStart ( 2 , '0' ) ;
71+
72+ return `${ formattedHours } :${ minutes } ${ suffix } ` ;
73+ }
74+
75+ const currentOutput6 = formatAs12HourClock ( "23:00" ) ;
76+ const targetOutput6 = "11:00 pm" ;
77+ console . assert (
78+ currentOutput6 === targetOutput6 ,
79+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
80+ ) ;
0 commit comments