Skip to content

Instantly share code, notes, and snippets.

@cdelahousse
Created June 3, 2012 02:35
Show Gist options
  • Save cdelahousse/2861041 to your computer and use it in GitHub Desktop.
Save cdelahousse/2861041 to your computer and use it in GitHub Desktop.
Binary clock version two, reimplemented to be shorter
function pad(str,num) { return str.length < num ? pad(0 + str,num) : str; }
function binaryClock(d) {
var bin = (pad('' + d.getHours(),2)
+ pad('' + d.getMinutes(),2)
+ pad('' + d.getSeconds(),2) ).split('') //string of time digits to array
.map(function (n) { return pad((+n).toString(2),4) }), //conv to binary and pad
i,j,str='';
for (i = 0; i<4; i++) {
for (j = 0; j<bin.length; j++) {
str += bin[j][i];
} str += '\n'; }
return str
}
console.log(binaryClock(new Date()));
@psimonyi
Copy link

psimonyi commented Jun 4, 2012

  • The recursive padding is clever, since you're expecting little padding.
  • Line 8 looks like the beginning of a statement. It isn't, but to notice that you have to distinguish between , and ; at the end of line 7 before the comment. You could just use a second var, or you could indent line 8 more. I think I'd use a second var statement and put a blank line before it, so the function has two parts (calculating, then reorienting).
  • The function that converts a single digit to BCD looks squeezed in. I'm not sure it's quite simple enough to be inline (though maybe it's just the punctuation in (+n) that bothers me) — plus, if you pulled it out and gave it a name, the code would be more self-documenting.
  • There are two places the number 4 comes up (for the bit-length of a BCD digit) that look like magic.
  • The formatting of line 12 looks odd. And line 14 is wanting a semicolon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment