File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
src/hackerrank/problem-solving Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 011: Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.
3
+ */
4
+
5
+ /*
6
+ * Complete the 'timeConversion' function below.
7
+ *
8
+ * The function is expected to return a STRING.
9
+ * The function accepts STRING s as parameter.
10
+ */
11
+
12
+ function timeConversion ( s ) {
13
+ // get time slice
14
+ // const [hours, minutes, modifier] = s.split(':');
15
+ // console.log(hours,minutes,modifier)
16
+ const time = s . match ( / ( \d + ) : ( \d + ) : ( \d + ) ( \w + ) / ) ;
17
+ let hours = time [ 1 ] ;
18
+ const minutes = time [ 2 ] ;
19
+ const seconds = time [ 3 ] ;
20
+ const modifier = time [ 4 ] ;
21
+
22
+ if ( hours === '12' ) {
23
+ hours = '00' ;
24
+ }
25
+
26
+ if ( modifier === 'PM' ) {
27
+ hours = parseInt ( hours , 10 ) + 12 ;
28
+ }
29
+
30
+ return `${ hours } :${ minutes } :${ seconds } ` ;
31
+ }
You can’t perform that action at this time.
0 commit comments