Changed around line 1
+ const binaryInput = document.getElementById('binary');
+ const decimalInput = document.getElementById('decimal');
+ const binaryError = document.getElementById('binary-error');
+
+ const validateBinary = (value) => {
+ return /^[01]+$/.test(value);
+ };
+
+ const binaryToDecimal = (binary) => {
+ return parseInt(binary, 2);
+ };
+
+ const decimalToBinary = (decimal) => {
+ return (decimal >>> 0).toString(2);
+ };
+
+ binaryInput.addEventListener('input', (e) => {
+ const value = e.target.value;
+
+ if (value === '') {
+ decimalInput.value = '';
+ binaryError.style.display = 'none';
+ return;
+ }
+
+ if (!validateBinary(value)) {
+ binaryError.textContent = 'Please enter a valid binary number (0s and 1s only)';
+ binaryError.style.display = 'block';
+ return;
+ }
+
+ binaryError.style.display = 'none';
+ decimalInput.value = binaryToDecimal(value);
+ });
+
+ decimalInput.addEventListener('input', (e) => {
+ const value = e.target.value;
+
+ if (value === '') {
+ binaryInput.value = '';
+ return;
+ }
+
+ binaryInput.value = decimalToBinary(value);
+ });