Created
June 3, 2012 02:35
-
-
Save cdelahousse/2861041 to your computer and use it in GitHub Desktop.
Binary clock version two, reimplemented to be shorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
,
and;
at the end of line 7 before the comment. You could just use a secondvar
, 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).(+n)
that bothers me) — plus, if you pulled it out and gave it a name, the code would be more self-documenting.