Last active
August 28, 2021 22:04
-
-
Save keif/56ce238ac72c9d5ae0daaed117baec7d to your computer and use it in GitHub Desktop.
Complete the timeConversion function in the editor below. It should return a new string representing the input time in 24 hour format. timeConversion has the following parameter(s): string s: a time in hour format Returns string: the time in hour format Input Format A single string that represents a time in -hour clock format (i.e.: or ). Constr…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const timeConversion = (s) => { | |
const seperator = `:` | |
const timeArr = s.slice(0, 8).split(seperator); | |
const hours = parseInt(timeArr[0], 10); | |
if (s.toUpperCase().indexOf(`PM`) > -1) { | |
// handle PM | |
timeArr[0] = (hours < 12) ? (hours + 12).toString() : hours.toString() | |
} else { | |
// handle AM | |
timeArr[0] = (hours === 12) ? "00" : timeArr[0].toString() | |
} | |
console.log(timeArr.join(seperator)) | |
return timeArr.join(seperator) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment