-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
45 lines (35 loc) · 1.17 KB
/
scripts.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
const binInput = document.getElementById("bin-number");
const errorSpan = document.getElementById("error-message");
const convertButton = document.getElementById("btn-convert");
const resultText = document.getElementById("result");
binInput.addEventListener("focus", function(){
resultText.innerHTML = "";
})
binInput.addEventListener("input", function(event){
const value = event.target.value;
if(value.match(/[^01]/) && value != "") {
errorSpan.classList.add("active");
convertButton.disabled = true;
} else {
errorSpan.classList.remove("active");
convertButton.disabled = value == "";
}
})
convertButton.addEventListener("click", () => {
const bin = binInput.value;
const dec = bin2Dec(bin);
console.log(bin, " => ", dec);
const res = bin + "<sub>(2)</sub>" + " = " + dec + "<sub>(10)</sub>";
resultText.classList.add("active");
resultText.innerHTML = res;
binInput.value = "";
convertButton.disabled = true;
})
function bin2Dec(bin) {
const length = bin.length;
let value = 0;
for(let i = 0; i < length; i++) {
value += bin[length - i - 1] * (2 ** i);
}
return value;
}