Skip to content

Instantly share code, notes, and snippets.

@psimonyi
Forked from cdelahousse/binclock.js
Created May 29, 2012 04:35
Show Gist options
  • Save psimonyi/2822537 to your computer and use it in GitHub Desktop.
Save psimonyi/2822537 to your computer and use it in GitHub Desktop.
JS Binary Clock
// vim: shiftwidth=8 noexpandtab softtabstop=0
/*
* Christian Delahousse
* Binary Clock
* May 27,2012
* Edited by Peter Simonyi 29 May 2012
*/
'use strict';
/*
// Return the binary representation of a decimal number.
function dec2bin(dec) {
return dec.toString(2);
}
*/
// Yes, this is a built-in operation! But let's reimplement it anyway.
// Return the binary representation of a decimal number.
// This is only good for nonnegative integers.
function dec2bin(dec) {
function lb(x) Math.log(x) / Math.log(2); // Log base 2
// Start with all zeros: there are floor(lb(dec)) + 1 bits needed.
// (This also ensures that dec2bin(0) === '0'.)
var bin = pad_number(0, Math.floor(lb(dec)) + 1).split('');
// Oh, JavaScript. (We need an array for writable subscript access.)
// The floor of the log tells you which bit (counting backwards) to set.
while (dec > 0) {
var bit = Math.floor(lb(dec));
bin[bin.length - bit - 1] = '1';
dec -= Math.pow(2, bit);
}
return bin.join('');
}
// Return an array [h, m, s] representing the current time.
function time() {
var date = new Date();
return [date.getHours(), date.getMinutes(), date.getSeconds()];
}
// Convert each base-10 digit of |number| to binary. Return an array.
// (Only works on nonnegative integers.)
function binary_coded_decimal(number) {
var digits = number.toString().split('');
return digits.map(dec2bin);
}
// Convert time to BCD and pad everything appropriately. With all this mapping,
// it would be nice to have array comprehensions and foreach!
function convertTimeDigits2Bin(/*array*/ time) {
function pad2(n) { return pad_number(n, 2); }
function pad4(n) { return pad_number(n, 4); }
function mappad4(x) { return x.map(pad4); }
return time.map(pad2).map(binary_coded_decimal).map(mappad4);
}
// Convert |time| (an array of numbers) to BCD and print it so each decimal
// digit (4 binary digits) takes 1 column and 4 rows.
function clockStr_oldversion(time) {
/*const*/ var BCD_WIDTH = 4;
var digits = flatten(convertTimeDigits2Bin(time));
var lines = ['', '', '', '']; // BCD_WIDTH empty strings
digits.forEach(function (digit) {
for (var i = 0; i < BCD_WIDTH; ++i) {
lines[i] += digit[i];
}
});
return lines.join('\n');
}
// Convert |time| (an array of numbers) to BCD and print it so each decimal
// digit (4 binary digits) takes 1 column and 4 rows.
function clockStr(time) {
var digits = flatten(convertTimeDigits2Bin(time));
// Convert the strings to arrays of characters.
digits = digits.map(function (x) x.split(''));
function shift(a) a.shift();
var rv = '';
while (digits[0].length) { // Assuming all digits are the same length!
rv += digits.map(shift).join('') + '\n';
}
return rv;
}
// Flatten arbitrarily-deeply-nested arrays to an array of non-array things.
function flatten(array) {
// If array isn't really an array, let it pass.
if (!Array.isArray(array)) return array;
return Array.prototype.concat.apply([], array.map(flatten));
}
// Return |num| as a string left-padded with '0' to |length| digits/characters
function pad_number(num, length) {
num = num.toString();
var padding = '';
while (padding.length < length - num.length) {
padding += '0';
}
return padding + num;
}
function main() {
console.log(clockStr(time()));
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment