Skip to content

Commit f51faf6

Browse files
Merge pull request #86 from wakidurrahman/feat/problem-solving-011
feat: problem solving
2 parents c6c1def + 362aeb4 commit f51faf6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)