-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
63 lines (54 loc) · 1.86 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var BinaryClock = {};
(function (B) {
"use strict";
/**
* Returns a string containing the binary form of a number.
* Always returns six characters (prepends smaller numbers with 0)
* Example converter(2) -> "000010"
*/
B.converter = function(number) {
var binary = number.toString(2);
return B.padWithZeros(binary, 6);
}
B.convertTime = function(timestamp) {
var hour = B.converter(timestamp.getHours());
var minute = B.converter(timestamp.getMinutes());
var second = B.converter(timestamp.getSeconds());
return hour + ":" + minute + ":" + second;
}
/**
* Returns a string with the number in question prepended with a number of
* zeros ('0') to any text to make sure it fills up the minimum length.
* Yes, this could be generalized a lot more and allow it to specify the
* characther being padded, but it works for this use case.
*
* Parameters:
* number - some number
* number_of_characters - will prepend zeros until the text reaches this length
*/
B.padWithZeros = function(number, number_of_characters) {
//I don't like to convert it right away, but then I don't have to do it
//x different places outside the method. Probably not ideal...
var text = number.toString();
while(text.length < number_of_characters) {
text = "0" + text;
}
return text;
}
B.tick = function () {
var currentTime = new Date();
var calculatedTime = B.convertTime(currentTime);
B.render(calculatedTime +
"<p>" +
B.padWithZeros(currentTime.getHours(), 2) + ":" +
B.padWithZeros(currentTime.getMinutes(), 2) + ":" +
B.padWithZeros(currentTime.getSeconds(), 2) +
"</p>");
}
B.render = function (text) {
document.getElementById('clock').innerHTML = text;
}
})(BinaryClock);
setInterval(function () {
BinaryClock.tick()
}, 1000);