-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.js
More file actions
40 lines (33 loc) · 1.41 KB
/
counter.js
File metadata and controls
40 lines (33 loc) · 1.41 KB
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
document.addEventListener('DOMContentLoaded', () => {
const leds = document.querySelectorAll('.led');
const binaryDisplay = document.querySelector('.binary-display');
const decimalDisplay = document.querySelector('.decimal-display');
const incrementBtn = document.getElementById('increment');
const decrementBtn = document.getElementById('decrement');
let binaryNumber = '0000';
function updateDisplay() {
binaryDisplay.textContent = `Binary: ${binaryNumber}`;
decimalDisplay.textContent = `Decimal: ${parseInt(binaryNumber, 2)}`;
leds.forEach((led, index) => {
const bit = binaryNumber[index];
led.classList.toggle('on', bit === '1');
led.textContent = bit;
led.setAttribute('aria-label', `Bit ${3 - index} is ${bit === '1' ? 'on' : 'off'}`);
});
}
function incrementBinary() {
const decimal = parseInt(binaryNumber, 2) + 1;
if (decimal > 15) return;
binaryNumber = decimal.toString(2).padStart(4, '0');
updateDisplay();
}
function decrementBinary() {
const decimal = parseInt(binaryNumber, 2) - 1;
if (decimal < 0) return;
binaryNumber = decimal.toString(2).padStart(4, '0');
updateDisplay();
}
incrementBtn.addEventListener('click', incrementBinary);
decrementBtn.addEventListener('click', decrementBinary);
updateDisplay();
});