-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
150 lines (125 loc) · 3.43 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const MainModule = (function() {
// set age to be calculated
const AGE = 50
const $page = $('html'),
$msg = $('#msg'),
$ageText = $('#ageTxt'),
$age = $('#age');
myTime = 10,
birthdayText = 'Happy Birthday, dear Father! :D';
let randColor,
randRed,
growRed = true,
passedTime = 0,
strLength = birthdayText.length;
return {
init: () => {
randRed = MainModule.getRandomRed();
MainModule.timerActions(10);
},
getRandomRed: () => {
let red = MainModule.getRandomVal(50, 100);
return red;
},
decimalToBinary: (number, binaryNumber = 0) => {
let val = MainModule.getClosestPowerOfTwo(number);
number -= Math.pow(2, val);
binaryNumber += Math.pow(10, val);
if(number === 0) {
return binaryNumber;
} else {
return MainModule.decimalToBinary(number, binaryNumber);
}
},
getClosestPowerOfTwo: (number) => {
if(number === 0) return null;
let i = 0, power = 1;
while(power <= number) {
power *= 2;
i++;
}
return (i - 1);
},
getRandomVal: (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
},
binNumberToBinaryOctetString: (binaryNumber) => {
// works for binary numbers of max 8 digits
let digitCount = MainModule.numOfDigits(binaryNumber), octString = "";
for(let i = 1; i <= 8 - digitCount; i++) {
octString += "0";
}
octString += binaryNumber;
return octString;
},
numberToBinaryOctetString: (number) => {
let binNumber = MainModule.decimalToBinary(number);
let octBinNumber = MainModule.binNumberToBinaryOctetString(binNumber);
return octBinNumber;
},
numOfDigits: (x) => {
return Math.max(Math.floor(Math.log10(Math.abs(x))), 0) + 1;
},
changeColor: () => {
if(growRed) {
if(randRed < 150) {
randRed++;
} else {
growRed = false;
randRed--;
}
} else {
if(randRed > 50) {
randRed--;
}
else {
growRed = true;
randRed++;
}
}
randColor = `rgb(${randRed},0,0)`;
$page.css({'background-color': randColor});
},
getNextLetter: (myString, pos) => {
if(pos <= strLength) {
pos++;
return myString[pos-1];
} else {
return null;
}
},
setAge: (currentNumber, number, timeInterval) => {
let currentAge = MainModule.numberToBinaryOctetString(currentNumber);
$age.text(currentAge);
if(currentNumber < number) {
currentNumber++;
setTimeout(() => {
MainModule.setAge(currentNumber, number, timeInterval - 10);
}, timeInterval);
}
},
timerActions: (timeInterval) => {
passedTime += timeInterval;
if(passedTime % 120 === 0) {
$msg.append(MainModule.getNextLetter( birthdayText, (passedTime / 120 - 1) ));
if(passedTime % 240 === 0) {
$msg.toggleClass('hidden');
}
if(passedTime / 120 - 1 === strLength) {
$ageText.fadeIn(1000, () => {
MainModule.setAge(1, AGE, 500);
});
}
}
MainModule.changeColor();
setTimeout(() => {
MainModule.timerActions(timeInterval);
} , timeInterval);
}
}
})();
$(document).ready(function() {
MainModule.init();
});