@@ -23,3 +23,68 @@ console.assert(
2323 currentOutput2 === targetOutput2 ,
2424 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2525) ;
26+ const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
27+ const targetOutput3 = "12:00 am" ;
28+ console . assert ( currentOutput3 === targetOutput3 ,
29+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
30+ )
31+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
32+ const targetOutput4 = "12:00 pm" ;
33+ console . assert ( currentOutput4 === targetOutput4 ,
34+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
35+ )
36+
37+
38+ // This is my version
39+ function TimeAs12hours ( time ) {
40+ const hours = Number ( time . slice ( 0 , 2 ) ) ;
41+ const minutes = time . slice ( 3 ) ;
42+ if ( hours === 0 ) {
43+ return `12:${ minutes } am` ;
44+ } else if ( hours > 12 ) {
45+ return `${ hours - 12 } :${ minutes } pm` ;
46+ } else if ( hours === 12 ) {
47+ return `${ time } pm` ;
48+ }
49+ return `${ time } am` ;
50+ }
51+
52+ let currentTime = TimeAs12hours ( "12:02" ) ;
53+ let targetTime = "12:02 pm" ;
54+ console . assert (
55+ currentTime === targetTime ,
56+ `current time:${ currentTime } , Target Time:${ targetTime } `
57+ ) ;
58+ currentTime = TimeAs12hours ( "10:30" ) ;
59+ targetTime = "10:30 am" ;
60+ console . assert (
61+ currentTime === targetTime ,
62+ `current time:${ currentTime } , Target Time:${ targetTime } `
63+ ) ;
64+
65+ currentTime = TimeAs12hours ( "04:30" ) ;
66+ targetTime = "04:30 am" ;
67+ console . assert (
68+ currentTime === targetTime ,
69+ `current time:${ currentTime } , Target Time:${ targetTime } `
70+ ) ;
71+
72+ currentTime = TimeAs12hours ( "13:30" ) ;
73+ targetTime = "1:30 pm" ;
74+ console . assert (
75+ currentTime === targetTime ,
76+ `current time:${ currentTime } , Target Time:${ targetTime } `
77+ ) ;
78+ currentTime = TimeAs12hours ( "23:09" ) ;
79+ targetTime = "11:09 pm" ;
80+ console . assert (
81+ currentTime === targetTime ,
82+ `current time:${ currentTime } , Target Time:${ targetTime } `
83+ ) ;
84+
85+ currentTime = TimeAs12hours ( "00:00" ) ;
86+ targetTime = "12:00 am" ;
87+ console . assert (
88+ currentTime === targetTime ,
89+ `current time:${ currentTime } , Target Time:${ targetTime } `
90+ ) ;
0 commit comments