-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (33 loc) · 857 Bytes
/
index.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
var entry =document.getElementById("entry");
var result=document.getElementById("result");
var cbtn=document.getElementsByClassName("myButton")[0];
var rbtn=document.getElementsByClassName("myButton")[1];
cbtn.addEventListener("click",() =>
{
if(entry.value=="")
alert("Binary input is Empty :(");
else
{
var answer=0,pow=1,i;
for(i=entry.value.length-1;i>=0;i--)
{
if(entry.value[i]=="1")
answer=answer+pow;
else if(entry.value[i]!="1"&&entry.value[i]!="0")
{
result.value="";
alert("Invalid Binary Number!!!");
break;
}
pow*=2;
}
if(i==-1)
result.value=answer;
}
}
);
rbtn.addEventListener("click", () =>
{
entry.value="";
result.value="";
});