diff --git a/src/hackerrank/problem-solving/algorithms-011-020.js b/src/hackerrank/problem-solving/algorithms-011-020.js new file mode 100644 index 0000000..2c167db --- /dev/null +++ b/src/hackerrank/problem-solving/algorithms-011-020.js @@ -0,0 +1,31 @@ +/** + * 011: Given a time in 12-hour AM/PM format, convert it to military (24-hour) time. + */ + +/* + * Complete the 'timeConversion' function below. + * + * The function is expected to return a STRING. + * The function accepts STRING s as parameter. + */ + +function timeConversion(s) { + // get time slice + // const [hours, minutes, modifier] = s.split(':'); + // console.log(hours,minutes,modifier) + const time = s.match(/(\d+):(\d+):(\d+)(\w+)/); + let hours = time[1]; + const minutes = time[2]; + const seconds = time[3]; + const modifier = time[4]; + + if (hours === '12') { + hours = '00'; + } + + if (modifier === 'PM') { + hours = parseInt(hours, 10) + 12; + } + + return `${hours}:${minutes}:${seconds}`; +}