44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const mint = time . slice ( - 2 ) ;
78 if ( hours > 12 ) {
89 return `${ hours - 12 } :00 pm` ;
10+ } else if ( hours === 0 ) {
11+ return `12:${ mint } am` ;
12+ } else if ( hours === 12 ) {
13+ return `12:${ mint } pm` ;
14+ } else {
15+ return `${ time } am` ;
916 }
10- return `${ time } am` ;
1117}
1218
1319const currentOutput = formatAs12HourClock ( "08:00" ) ;
@@ -16,10 +22,35 @@ console.assert(
1622 currentOutput === targetOutput ,
1723 `current output: ${ currentOutput } , target output: ${ targetOutput } `
1824) ;
19-
2025const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
2126const targetOutput2 = "11:00 pm" ;
2227console . assert (
2328 currentOutput2 === targetOutput2 ,
2429 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2530) ;
31+
32+ const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
33+ const targetOutput3 = "12:00 am" ;
34+ console . assert (
35+ currentOutput3 === targetOutput3 ,
36+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
37+ ) ;
38+
39+ const currentOutput4 = formatAs12HourClock ( "16:00" ) ;
40+ const targetOutput4 = "04:00 pm" ;
41+ console . assert (
42+ currentOutput4 === targetOutput4 ,
43+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
44+ ) ;
45+
46+ const currentOutput5 = formatAs12HourClock ( "20:00" ) ;
47+ const targetOutput5 = "08:00 pm" ;
48+ console . assert (
49+ currentOutput5 === targetOutput5 ,
50+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
51+ ) ;
52+
53+ console . log ( formatAs12HourClock ( "08:00" ) ) ;
54+ console . log ( formatAs12HourClock ( "23:00" ) ) ;
55+ console . log ( formatAs12HourClock ( "00:00" ) ) ;
56+ console . log ( formatAs12HourClock ( "16:00" ) ) ;
0 commit comments