Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Morse Clock by kurosawa4434
"use strict";
function morseClock(data) {
const [h, m, s] = data.split(':').map(d=>parseInt(d, 10));
const [h1, h2] = [Math.floor(h/10), h % 10];
const [m1, m2] = [Math.floor(m/10), m % 10];
const [s1, s2] = [Math.floor(s/10), s % 10];
const morse = (n, dg)=>{
const m = n.toString(2).replace(/0/g, '.').replace(/1/g, '-');
return '.'.repeat((dg-m.length)) + m;
};
return (morse(h1, 2) + ' ' + morse(h2, 4) + ' : ' +
morse(m1, 3) + ' ' + morse(m2, 4) + ' : ' +
morse(s1, 3) + ' ' + morse(s2, 4));
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(morseClock("10:37:49"), ".- .... : .-- .--- : -.. -..-", "1st");
assert.equal(morseClock("21:34:56"), "-. ...- : .-- .-.. : -.- .--.", "2nd");
assert.equal(morseClock("00:1:02"), ".. .... : ... ...- : ... ..-.", "3rd");
assert.equal(morseClock("23:59:59"), "-. ..-- : -.- -..- : -.- -..-", "4th");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
July 25, 2017