Created
May 28, 2012 00:05
-
-
Save cdelahousse/2816461 to your computer and use it in GitHub Desktop.
JS Binary Clock
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
/*jslint devel: true, bitwise: true, continue: true, debug: true, eqeq: true, | |
plusplus: true, vars: true, white: true */ | |
/* | |
* Christian Delahousse | |
* Binary Clock | |
* May 27,2012 | |
*/ | |
(function () { | |
'use strict'; | |
function dec2bin(dec) { | |
var binstring = [], | |
remainder, | |
quotient, | |
bitindex = 0; | |
//If zero, thanthe binary representation is zero | |
if (dec === 0) { | |
return '0'; | |
} | |
quotient = dec; | |
//Do conversion alg. until quotient is zero | |
while (quotient !== 0) { | |
binstring[bitindex] = | |
(quotient % 2 === 0) ? 0 : 1; | |
quotient = Math.floor(quotient / 2); //Integer division removes remainder | |
bitindex += 1; | |
} | |
return binstring.reverse().join(""); | |
} | |
//Get current time, input to array, return that array | |
function time() { | |
var current_time = [], | |
date = new Date(); | |
current_time.push(date.getHours()); | |
current_time.push(date.getMinutes()); | |
current_time.push(date.getSeconds()); | |
return current_time; | |
} | |
function convertTimeDigits2Bin(time) { | |
var binary_fulltime = [], | |
binary_num = [], | |
binary_digit, | |
current_num, | |
current_digit, | |
len, | |
i; | |
//Convert every digit of time into binary | |
//return array containing digits [ [ hour, hour ], [min,min], ....] | |
while (time.length > 0) { | |
current_num = time.shift().toString(); | |
len = current_num.length; | |
binary_num = []; //Contains separate digits | |
if (len < 2) { | |
binary_num.push('0'); //Add leading zero | |
} | |
for (i = 0 ; i < len ; i++) { | |
current_digit = current_num[i]; | |
binary_digit = dec2bin( current_digit ); | |
binary_num.push(binary_digit); | |
} | |
binary_fulltime.push(binary_num); | |
} | |
return binary_fulltime; | |
} | |
//Flatten list | |
function flatten(/*array*/ obj) { | |
if (obj.length == 0){ | |
return []; | |
} | |
if (obj.constructor !== Array) { | |
return obj; | |
} | |
else { | |
return obj.concat(flatten(obj.shift()), flatten(obj)); | |
} | |
} | |
function clockStr(time) { | |
var DIGITS_MAX_NUM = 4; | |
var time_binary = convertTimeDigits2Bin(time), | |
time_padded_binary = padNum(time_binary,DIGITS_MAX_NUM, "0"), | |
flat_digits = flatten(time_padded_binary); | |
var str = "", index_num, index_digit; | |
for (index_digit = 0; index_digit < DIGITS_MAX_NUM; index_digit++) { | |
for (index_num = 0; index_num < flat_digits.length; index_num++) { | |
str += flat_digits[index_num][index_digit]; | |
} | |
str += "\n"; | |
} | |
return str; | |
} | |
function padNum(/*array*/obj, /*int*/ max_pad,/*string*/ pad_char) { | |
//If obj is a string (atomic) -->> not array | |
if (Object.prototype.toString.call ( obj ) != '[object Array]') { | |
while (obj.length < max_pad) { | |
obj = pad_char + obj ; | |
} | |
return obj; | |
//Object must be array | |
} else { | |
var i, | |
a = []; | |
for( i = 0; i < obj.length; i++) { | |
a.push(padNum(obj[i], max_pad, pad_char)); | |
} | |
return a; | |
} | |
} | |
function main() { | |
console.log(clockStr(time())); | |
} | |
main(); | |
})(); |
Indeed. I thought the output was somehow being wrapped at 6 bits, but still proceeding L-R, T-B as usual... and it took a while to figure out why you were iterating over a 2D array the "wrong" way.
Linted and updated.
I kinda went overboard while trying to generalizing everything. I could have easily avoid functions like flatten() and what not.
Where did I go through a 2D array backwards?
Output:
000001
001000
000100
000001
Represents
HH:MM:SS
00:42:09
I initially read "000001 001000 ..." instead of "0000 0000 ..." because usually you read left to right before top to bottom.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Documentation is important. For example: I ran this code, but I have no idea what I'm looking at. You should convince me to read the code by making me wonder how it works, rather than making me wonder what the output means.