Skip to content

Instantly share code, notes, and snippets.

@topeogunleye
Created May 6, 2022 07:12
Show Gist options
  • Save topeogunleye/0c942681c145148827614b6d8ab4cbd5 to your computer and use it in GitHub Desktop.
Save topeogunleye/0c942681c145148827614b6d8ab4cbd5 to your computer and use it in GitHub Desktop.
Time Conversion HackerRank Challenge
function timeConversion(s) {
// Write your code here
// if hour is 12am, return 00
// if hour is between 1am to 11:59am, return 1 to 11:59
// if time is 12pm, return 12
// if hour is between 1pm to 11:59pm, return 1 + 12 to 11:59 + 12
// Military Hour
let militaryHour = '';
let hour = s.substring(0, 2);
let amPM = s.charAt(8)
console.log(hour, amPM)
if (amPM == 'A') {
if (hour == '12') {
militaryHour = '00';
} else {
militaryHour = hour
}
} else {
if (hour == '12') {
militaryHour = '12';
} else {
militaryHour = parseInt(hour, 10) + 12
}
}
return militaryHour + s.substring(2, 8)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment