@@ -14,10 +14,14 @@ function formatAs12HourClock(time) {
1414 if ( minutes < 60 ) {
1515 if ( hours >= 24 ) {
1616 return `please check your input hour` ;
17- } else if ( hours > 12 ) {
17+ } else if ( hours > 12 && hours <= 21 ) {
18+ return `0${ hours - 12 } :${ slicedMinutes } pm` ;
19+ } else if ( hours > 21 ) {
1820 return `${ hours - 12 } :${ slicedMinutes } pm` ;
1921 } else if ( hours === 0 ) {
2022 return `12:${ slicedMinutes } am` ;
23+ } else if ( hours === 12 ) {
24+ return `12:${ slicedMinutes } pm` ;
2125 } else {
2226 return `${ slicedHours } :${ slicedMinutes } am` ;
2327 }
@@ -26,75 +30,108 @@ function formatAs12HourClock(time) {
2630 }
2731}
2832
29- const currentOutput1 = formatAs12HourClock ( "00:60 " ) ;
30- const targetOutput1 = "please check your input minute " ;
33+ const currentOutput1 = formatAs12HourClock ( "00:00 " ) ;
34+ const targetOutput1 = "12:00 am " ;
3135console . assert (
3236 currentOutput1 === targetOutput1 ,
3337 `current output: ${ currentOutput1 } , target output: ${ targetOutput1 } ` ,
3438 "dude whats wrong" ,
3539) ;
3640
37- const currentOutput2 = formatAs12HourClock ( "24:45 " ) ;
38- const targetOutput2 = "please check your input hour " ;
41+ const currentOutput2 = formatAs12HourClock ( "13:00 " ) ;
42+ const targetOutput2 = "01:00 pm " ;
3943console . assert (
4044 currentOutput2 === targetOutput2 ,
4145 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
4246 "dude whats wrong" ,
4347) ;
4448
45- const currentOutput3 = formatAs12HourClock ( "23:59 " ) ;
46- const targetOutput3 = "11:59 pm" ;
49+ const currentOutput3 = formatAs12HourClock ( "21:00 " ) ;
50+ const targetOutput3 = "09:00 pm" ;
4751console . assert (
4852 currentOutput3 === targetOutput3 ,
4953 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
5054 "dude whats wrong" ,
5155) ;
5256
53- const currentOutput4 = formatAs12HourClock ( "07:30 " ) ;
54- const targetOutput4 = "07:30 am " ;
57+ const currentOutput4 = formatAs12HourClock ( "22:00 " ) ;
58+ const targetOutput4 = "10:00 pm " ;
5559console . assert (
5660 currentOutput4 === targetOutput4 ,
5761 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
5862 "dude whats wrong" ,
5963) ;
6064
61- const currentOutput5 = formatAs12HourClock ( "22:45 " ) ;
62- const targetOutput5 = "10:45 pm" ;
65+ const currentOutput5 = formatAs12HourClock ( "23:59 " ) ;
66+ const targetOutput5 = "11:59 pm" ;
6367console . assert (
6468 currentOutput5 === targetOutput5 ,
6569 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } ` ,
6670 "dude whats wrong" ,
6771) ;
6872
69- const currentOutput6 = formatAs12HourClock ( "00:20 " ) ;
70- const targetOutput6 = "12:20 am " ;
73+ const currentOutput6 = formatAs12HourClock ( "12:60 " ) ;
74+ const targetOutput6 = "please check your input minute " ;
7175console . assert (
7276 currentOutput6 === targetOutput6 ,
7377 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } ` ,
7478 "dude whats wrong" ,
7579) ;
7680
77- const currentOutput7 = formatAs12HourClock ( "12:45 " ) ;
78- const targetOutput7 = "12:45 pm " ;
81+ const currentOutput7 = formatAs12HourClock ( "24:00 " ) ;
82+ const targetOutput7 = "please check your input hour " ;
7983console . assert (
8084 currentOutput7 === targetOutput7 ,
8185 `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } ` ,
8286 "dude whats wrong" ,
8387) ;
8488
85- const currentOutput8 = formatAs12HourClock ( "00:0 " ) ;
89+ const currentOutput8 = formatAs12HourClock ( "100:00 " ) ;
8690const targetOutput8 = "wrong format input" ;
8791console . assert (
8892 currentOutput8 === targetOutput8 ,
8993 `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } ` ,
9094 "dude whats wrong" ,
9195) ;
9296
93- console . log ( formatAs12HourClock ( "00:60" ) ) ;
94- console . log ( formatAs12HourClock ( "24:45" ) ) ;
97+ const currentOutput9 = formatAs12HourClock ( "0:00" ) ;
98+ const targetOutput9 = "wrong format input" ;
99+ console . assert (
100+ currentOutput9 === targetOutput9 ,
101+ `current output: ${ currentOutput9 } , target output: ${ targetOutput9 } ` ,
102+ "dude whats wrong" ,
103+ ) ;
104+
105+ const currentOutput10 = formatAs12HourClock ( "tt:00" ) ;
106+ const targetOutput10 = "wrong format input" ;
107+ console . assert (
108+ currentOutput10 === targetOutput10 ,
109+ `current output: ${ currentOutput10 } , target output: ${ targetOutput10 } ` ,
110+ "dude whats wrong" ,
111+ ) ;
112+
113+ console . log ( formatAs12HourClock ( "00:00" ) ) ;
114+ console . log ( formatAs12HourClock ( "12:00" ) ) ;
115+ console . log ( formatAs12HourClock ( "13:00" ) ) ;
116+ console . log ( formatAs12HourClock ( "21:00" ) ) ;
117+ console . log ( formatAs12HourClock ( "22:00" ) ) ;
95118console . log ( formatAs12HourClock ( "23:59" ) ) ;
96- console . log ( formatAs12HourClock ( "07:30" ) ) ;
97- console . log ( formatAs12HourClock ( "22:45" ) ) ;
98- console . log ( formatAs12HourClock ( "00:20" ) ) ;
99- console . log ( formatAs12HourClock ( "12:45" ) ) ;
100- console . log ( formatAs12HourClock ( "00:0" ) ) ;
119+ console . log ( formatAs12HourClock ( "12:60" ) ) ;
120+ console . log ( formatAs12HourClock ( "24:00" ) ) ;
121+ console . log ( formatAs12HourClock ( "100:00" ) ) ;
122+ console . log ( formatAs12HourClock ( "0:00" ) ) ;
123+ console . log ( formatAs12HourClock ( "tt:00" ) ) ;
124+
125+ // /^\d{2}:\d{2}$/ so this is regular expression or regex I used them to validate the input time format
126+ // it is enclosed with /.../ telling the JS it is regular expression
127+ // ^ this means check from the start of the string
128+ // \d this refers single digit from 0 to 9
129+ // {2} means two times (\d{2} .....two single digits)
130+ // : simply checks with :
131+ // the same like before \d{2} checks two single digits
132+ // $ checking ends
133+
134+ // I preferred this because it will strictly check the format of the input with the desired input format
135+ // so that the following process will continue with out error, and it saves a lot of time
136+
137+ // It is rigid and does not check the value ....it only checks the format
0 commit comments