Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] Broken Clock by SaintDron
"use strict";
function toSeconds(desc){
const units = {'s': 1, 'm': 60, 'h': 3600};
let unit = desc.substr(desc.indexOf(' ') + 1, 1);
return parseInt(desc) * units[unit];
}
function brokenClock(startingTime, wrongTime, errorDescription){
let dev = errorDescription.slice(0, errorDescription.indexOf('at') - 1),
period = errorDescription.slice(errorDescription.indexOf('at') + 3),
ratio = toSeconds(period) / (toSeconds(period) + toSeconds(dev)),
startingTimeStamp = Date.parse(`1970-01-01T${startingTime}`),
wrongTimeStamp = Date.parse(`1970-01-01T${wrongTime}`),
correctTimeStamp = startingTimeStamp + (wrongTimeStamp - startingTimeStamp) * ratio,
correctTime = new Date(correctTimeStamp).toString();
return correctTime.substr(16, 8);
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(brokenClock('00:00:00', '00:00:15', '+5 seconds at 10 seconds'), '00:00:10', "First example");
assert.equal(brokenClock('06:10:00', '06:10:15', '-5 seconds at 10 seconds'), '06:10:30', "Second example");
assert.equal(brokenClock('13:00:00', '14:01:00', '+1 second at 1 minute'), '14:00:00', "Third example");
assert.equal(brokenClock('01:05:05', '04:05:05', '-1 hour at 2 hours'), '07:05:05', "Fourth example");
assert.equal(brokenClock('00:00:00', '00:00:30', '+2 seconds at 6 seconds'), '00:00:22', "Fifth example");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
March 3, 2018
Comments: