This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_to_decimal.js
284 lines (272 loc) · 14.4 KB
/
binary_to_decimal.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* file: binary_to_decimal.js
* type: JavaScript
* date: 10_JULY_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
/**
* Display a time stamped message inside of the div element whose id is "output".
*
* Set each of the eight light bulbs to "OFF" rather rather than to "ON".
*
* Set each of the eight binary digit values to zero (0) rather than to one (1).
*
* Set each of the buttons on the web page to visible rather than to hidden.
*/
function initialize_application() {
try {
const time_point = Date.now(), p0 = ('<' + 'p' + '>'), p1 = ('<' + '/' + 'p' + '>');
let light_bulb_off = '<' + 'img src="light_bulb_off.png" width="60"' + '>', i = 0;
const message = "The initialize_application() function was called at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).";
console.log(message);
document.getElementById("output").innerHTML = p0 + message + p1;
for (i = 0; i < 8; i += 1) {
document.getElementById("light_bulb_" + i).innerHTML = light_bulb_off;
document.getElementById("bit_" + i).innerHTML = 0;
document.getElementById("switch_" + i).style.display = "block";
}
document.getElementById("binary_to_decimal_button").style.display = "inline";
}
catch(exception) {
console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named initialize_application(): " + exception);
}
}
/**
* Change the binary state represented by the Nth light bulb in the sequence of 8 light bulbs to its only alternative state.
*
* If the Nth bit value is set to 0, the Nth light bulb state is assumed to be "OFF".
*
* If the Nth bit value is set to 1, the Nth light bulb state is assumed to be "ON".
*
* When the switch for the Nth light bulb is clicked, the following will occur:
*
* If the Nth bit is set to 0, the Nth bit will be set to 1 and the Nth light bulb state will be set to "ON".
*
* If the Nth bit is set to 1, the Nth bit will be set to 0 and the Nth light bulb state will be set to "OFF".
*
* @param {Number} N is assumed to be a base-ten integer no smaller than 0 and no larger than 7.
*/
function binary_switch(N) {
try {
const p0 = ('<' + 'p' + '>'), p1 = ('<' + '/' + 'p' + '>');
let bit = undefined, light_bulb_image = undefined, light_bulb_off = '<' + 'img src="light_bulb_off.png" width="60"' + '>', light_bulb_on = '<' + 'img src="light_bulb_on.png" width="60"' + '>';
bit = parseInt(document.getElementById("bit_" + N).innerHTML);
if (bit === 0) {
document.getElementById("bit_" + N).innerHTML = 1;
document.getElementById("light_bulb_" + N).innerHTML = light_bulb_on;
}
else {
document.getElementById("bit_" + N).innerHTML = 0;
document.getElementById("light_bulb_" + N).innerHTML = light_bulb_off;
}
}
catch(exception) {
console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named binary_switch(N): " + exception);
}
}
/**
* Change the binary state represented by the 7th (i.e. seventh) light bulb in the array of 8 light bulbs to its only alternative state.
*
* Note that the 7th light bulb represents the highest order bit and is the leftmost light bulb in the array.
*
* If the 7th bit value is set to 0, the 7th light bulb state is assumed to be "OFF".
*
* If the 7th bit value is set to 1, the 7th light bulb state is assumed to be "ON".
*
* When the switch for the 7th light bulb is clicked, the following will occur:
*
* If the 7th bit is set to 0, then the Nth bit will be set to 1 and the 7th light bulb state will be set to "ON".
*
* Otherwise, the 7th bit will be set to 0 and the 7th light bulb state will be set to "OFF".
*/
function switch_7() {
const time_point = Date.now();
console.log("The switch_7 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(7);
}
/**
* Change the binary state represented by the 6th (i.e. sixth) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 6th bit value is set to 0, the 6th light bulb state is assumed to be "OFF".
*
* If the 6th bit value is set to 1, the 6th light bulb state is assumed to be "ON".
*
* When the switch for the 6th light bulb is clicked, the following will occur:
*
* If the 6th bit is set to 0, then the 6th bit will be set to 1 and the 6th light bulb state will be set to "ON".
*
* Otherwise, the 6th bit will be set to 0 and the 6th light bulb state will be set to "OFF".
*/
function switch_6() {
const time_point = Date.now();
console.log("The switch_6 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(6);
}
/**
* Change the binary state represented by the 5th (i.e. fifth) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 5th bit value is set to 0, the 5th light bulb state is assumed to be "OFF".
*
* If the 5th bit value is set to 1, the 5th light bulb state is assumed to be "ON".
*
* When the switch for the 5th light bulb is clicked, the following will occur:
*
* If the 5th bit is set to 0, then the 5th bit will be set to 1 and the 5th light bulb state will be set to "ON".
*
* Otherwise, the 5th bit will be set to 0 and the 5th light bulb state will be set to "OFF".
*/
function switch_5() {
const time_point = Date.now();
console.log("The switch_5 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(5);
}
/**
* Change the binary state represented by the 4th (i.e. fourth) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 4th bit value is set to 0, the 4th light bulb state is assumed to be "OFF".
*
* If the 4th bit value is set to 1, the 4th light bulb state is assumed to be "ON".
*
* When the switch for the 4th light bulb is clicked, the following will occur:
*
* If the 4th bit is set to 0, then the 4th bit will be set to 1 and the 4th light bulb state will be set to "ON".
*
* Otherwise, the 4th bit will be set to 0 and the 4th light bulb state will be set to "OFF".
*/
function switch_4() {
const time_point = Date.now();
console.log("The switch_4 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(4);
}
/**
* Change the binary state represented by the 3rd (i.e. third) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 3rd bit value is set to 0, the 3rd light bulb state is assumed to be "OFF".
*
* If the 3rd bit value is set to 1, the 3rd light bulb state is assumed to be "ON".
*
* When the switch for the 3rd light bulb is clicked, the following will occur:
*
* If the 3rd bit is set to 0, then the 3rd bit will be set to 1 and the 3rd light bulb state will be set to "ON".
*
* Otherwise, the 3rd bit will be set to 0 and the 3rd light bulb state will be set to "OFF".
*/
function switch_3() {
const time_point = Date.now();
console.log("The switch_3 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(3);
}
/**
* Change the binary state represented by the 2nd (i.e. second) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 2nd bit value is set to 0, the 2nd light bulb state is assumed to be "OFF".
*
* If the 2nd bit value is set to 1, the 2nd light bulb state is assumed to be "ON".
*
* When the switch for the 2nd light bulb is clicked, the following will occur:
*
* If the 2nd bit is set to 0, then the 2nd bit will be set to 1 and the 2nd light bulb state will be set to "ON".
*
* Otherwise, the 2nd bit will be set to 0 and the 2nd light bulb state will be set to "OFF".
*/
function switch_2() {
const time_point = Date.now();
console.log("The switch_2 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(2);
}
/**
* Change the binary state represented by the 1st (i.e. first) light bulb in the array of 8 light bulbs to its only alternative state.
*
* If the 1st bit value is set to 0, the 1st light bulb state is assumed to be "OFF".
*
* If the 1st bit value is set to 1, the 1st light bulb state is assumed to be "ON".
*
* When the switch for the 1st light bulb is clicked, the following will occur:
*
* If the 1st bit is set to 0, then the 1st bit will be set to 1 and the 1st light bulb state will be set to "ON".
*
* Otherwise, the 1st bit will be set to 0 and the 1st light bulb state will be set to "OFF".
*/
function switch_1() {
const time_point = Date.now();
console.log("The switch_1 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(1);
}
/**
* Change the binary state represented by the 0th (i.e. zeroth) light bulb in the array of 8 light bulbs to its only alternative state.
*
* Note that the 0th light bulb represents the lowest order bit and is the rightmost light bulb in the array.
*
* If the 0th bit value is set to 0, the 0th light bulb state is assumed to be "OFF".
*
* If the 0th bit value is set to 1, the 0th light bulb state is assumed to be "ON".
*
* When the switch for the 1st light bulb is clicked, the following will occur:
*
* If the 0th bit is set to 0, then the 0th bit will be set to 1 and the 1st light bulb state will be set to "ON".
*
* Otherwise, the 0th bit will be set to 0 and the 0th light bulb state will be set to "OFF".
*/
function switch_0() {
const time_point = Date.now();
console.log("The switch_0 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
binary_switch(0);
}
/**
* Hide all buttons except for the RESET button.
*
* Convert the input binary number (represented by the current configuration of eight light bulbs)
* to its logically equivalent decimal output number and
* display the arithmetic steps used to convert the input value to the output value inside the div element whose id is "output".
*/
function binary_to_decimal() {
try {
const time_point = Date.now(), p0 = '<' + 'p' + '>', p1 = '<' + '/' + 'p' + '>', s0 = '<' + 'span class="console"' + '>', s1 = '<' + '/' + 'span' + '>';
let i = 0, binary_digits_string = "", decimal_output_number = 0, decimal_term_value = 0, arithmetic_steps = "", message = "";
message = "The BINARY_TO_DECIMAL button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).";
console.log(message);
document.getElementById("output").innerHTML += p0 + message + p1;
for (i = 0; i < 8; i += 1) document.getElementById("switch_" + i).style.display = "none";
document.getElementById("binary_to_decimal_button").style.display = "none";
for (i = 7; i > -1; i -= 1) binary_digits_string += document.getElementById("bit_" + i).innerHTML;
console.log("The input binary digit sequence is " + binary_digits_string + ".");
for (i = 7; i > -1; i -= 1) {
decimal_term_value = parseInt(binary_digits_string[i]) * Math.pow(2, 7 - i);
decimal_output_number += decimal_term_value;
}
console.log("The output decimal digit sequence is " + decimal_output_number + ".");
arithmetic_steps += p0 + "binary_input: " + s0 + binary_digits_string + s1 + "." + p1;
arithmetic_steps += p0 + "decimal_output: " + decimal_output_number + "." + p1;
arithmetic_steps += p0 + "arithmetic_steps: " + p1;
arithmetic_steps += p0 + decimal_output_number + " = " + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[0] + s1 + " * (2 ^ 7)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[1] + s1 + " * (2 ^ 6)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[2] + s1 + " * (2 ^ 5)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[3] + s1 + " * (2 ^ 4)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[4] + s1 + " * (2 ^ 3)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[5] + s1 + " * (2 ^ 2)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[6] + s1 + " * (2 ^ 1)) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[7] + s1 + " * (2 ^ 0)) = " + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[0] + s1 + " * 128) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[1] + s1 + " * 64) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[2] + s1 + " * 32) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[3] + s1 + " * 16) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[4] + s1 + " * 8) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[5] + s1 + " * 4) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[6] + s1 + " * 2) +" + p1;
arithmetic_steps += p0 + "(" + s0 + binary_digits_string[7] + s1 + " * 1) = " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[0]) * 128 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[1]) * 64 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[2]) * 32 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[3]) * 16 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[4]) * 8 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[5]) * 4 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[6]) * 2 + " + " + p1;
arithmetic_steps += p0 + parseInt(binary_digits_string[7]) * 1 + "." + p1;
document.getElementById("output").innerHTML += arithmetic_steps;
}
catch(exception) {
console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named binary_to_decimal(): " + exception);
}
}