-
Notifications
You must be signed in to change notification settings - Fork 0
/
scipt.js
29 lines (27 loc) · 918 Bytes
/
scipt.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
let decInp = document.getElementById('dec-inp');
let binInp = document.getElementById('bin-inp');
let errorMsg = document.getElementById('error-msg');
decInp.addEventListener('input', () => {
let decValue = parseInt(decInp.value);
//Converts the decimal value to binary
binInp.value = decValue.toString(2);
})
binInp.addEventListener('input', () => {
let binValue = binInp.value;
//if the binary number is valid convert it to decimal
if (binValidator(binValue)) {
decInp.value = parseInt(binValue, 2);
errorMsg.textContent = '';
} else {
errorMsg.textContent ='Please Enter a Valid Binary Input'
}
// function to check if binary number is valid
function binValidator(num) {
for (let i = 0; i < num.length; i++) {
if (num[i] != '0' && num[i] != '1') {
return false
}
}
return true
}
})