Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
match() solution in Clear category for [old] Time Converter (12h to 24h) by kurosawa4434
"use strict";
function timeConverter(dayTime) {
const [_, h, mi, am_pm] = dayTime.match(/(\d+)(:\d+) ([ap])\.m\./);
let hour = parseInt(h, 10);
if (hour !== 12 && am_pm === 'p')
hour += 12;
if (hour === 12 && am_pm === 'a')
hour = 0;
return (hour>9 ? '': '0') + hour + mi;
}
/*
def time_converter(time):
h, mi, ap = match('(\d+)(:\d+) ([ap])\.m\.', time).groups()
h = int(h)
if h != 12 and ap == 'p':
h += 12
if h == 12 and ap == 'a':
h = 0
return f'{h:02}{mi}'
*/
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(timeConverter('12:30 p.m.'))
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(timeConverter('12:30 p.m.'), '12:30')
assert.equal(timeConverter('9:00 a.m.'), '09:00')
assert.equal(timeConverter('11:15 p.m.'), '23:15')
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
July 6, 2018
Comments: