-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDay 09 - Binary Calculator.js
142 lines (120 loc) Β· 2.95 KB
/
Day 09 - Binary Calculator.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
JS:
var resultScreen=document.getElementById("res");
var result=0;
var operatorsSeq="";
function clickZero()
{
resultScreen.innerHTML+="0";
}
function clickOne()
{
resultScreen.innerHTML+="1";
}
function clickSum()
{
operatorSeq="+";
result=parseInt(resultScreen.innerHTML,2);
resultScreen.innerHTML+="+";
}
function clickSub()
{
operatorSeq="-";
resultScreen.innerHTML+="-";
}
function clickMul()
{
operatorSeq="*";
result=parseInt(resultScreen.innerHTML,2);
resultScreen.innerHTML+="*";
}
function clickDiv()
{
operatorSeq="/";
resultScreen.innerHTML+="/";
}
function clickEql()
{
var ans=0;
if(operatorSeq=='+')
{
var i =(resultScreen.innerHTML).indexOf("+");
var operand2=parseInt((resultScreen.innerHTML).substr(i+1),2);
ans =result+operand2;
}
else if(operatorSeq=='-')
{
var i =(resultScreen.innerHTML).indexOf("-");
var operand2=parseInt((resultScreen.innerHTML).substr(i+1),2);
ans =result-operand2;
}
else if(operatorSeq=='*')
{
var i =(resultScreen.innerHTML).indexOf("*");
var operand2=parseInt((resultScreen.innerHTML).substr(i+1),2);
ans =result*operand2;
}
else if(operatorSeq=='/')
{
var i =(resultScreen.innerHTML).indexOf("/");
var operand2=parseInt((resultScreen.innerHTML).substr(i+1),2);
ans =result/operand2;
}
resultScreen.innerHTML=ans.toString(2);
}
function clickClear()
{
resultScreen.innerHTML="";
}
CSS:
body{
width:33%;
}
#res{
background-color:lightgray;
border:solid;
height:48px;
font-size:20px;
}
#btn0,#btn1{
background-color:lightgreen;
color:brown;
}
#btnClr,#btnEql{
background-color:darkgreen;
color:white;
}
#btnSum,#btnSub,#btnMul,#btnDiv{
background-color:black;
color:red;
}
button{
width:25%;
height:36px;
font-size:18px;
margin:0px;
float:left;
}
HTML:
<!-- Enter your HTML code here -->
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/binaryCalculator.css" type="text/css">
<meta charset="utf-8">
<title>Binary Calculator</title>
</head>
<body>
<div id="res"></div>
<div id="btns">
<button id="btn0" onclick="clickZero()">0</button>
<button id="btn1" onclick="clickOne()">1</button>
<button id="btnClr" onclick="clickClear()">C</button>
<button id="btnEql" onclick="clickEql()">=</button>
<button id="btnSum" onclick="clickSum()">+</button>
<button id="btnSub" onclick="clickSub()">-</button>
<button id="btnMul" onclick="clickMul()">*</button>
<button id="btnDiv" onclick="clickDiv()">/</button>
</div>
<script src="js/binaryCalculator.js" type="text/javascript"></script>
</body>
</html>