-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (32 loc) · 1.08 KB
/
script.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
//Initial References
let decInp = document.getElementById("dec-inp");
let binInp = document.getElementById("bin-inp");
let errorMsg = document.getElementById("error-msg");
//Convert decimal to binary when user inputs in the decimal field
decInp.addEventListener("input", () => {
let decValue = parseInt(decInp.value);
//Converts the decimal value to binary
binInp.value = decValue.toString(2);
});
//Convert binary to decimal when user inputs in the binary field
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 display an error message
else {
errorMsg.textContent = "Please Enter An Valid Binary Input";
}
//Function to check if the binary number is valid i.e it does not contain any number other than 0 and 1
function binValidator(num) {
for (let i = 0; i < num.length; i++) {
if (num[i] != "0" && num[i] != "1") {
return false;
}
}
return true;
}
});