Task:
Implement a simple calculator that performs the following operations on binary numbers: addition, subtraction, multiplication, and division. Note that division operation must be integer division only;
The calculator’s initial state must look like this:

- Styling. The document’s elements must have the following styles:
body
has awidth
of33%
.res
has abackground-color
oflightgray
, aborder
that issolid
, aheight
of48px
, and afont-size
of20px
.btn0
andbtn1
have abackground-color
oflightgreen
and acolor
ofbrown
.btnClr
andbtnEql
have abackground-color
ofdarkgreen
and acolor
ofwhite
.btnSum
,btnSub
,btnMul
, andbtnDiv
have abackground-color
ofblack
, acolor
ofred
.- All the buttons in
btns
have awidth
of25%
, aheight
of36px
, afont-size
of18px
,margin
of0px
, andfloat
valueleft
.
The .js
and .css
files are in different directories, so use the link tag to provide the CSS file path and the script tag to provide the JS file path:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/binaryCalculator.css" type="text/css">
</head>
<body>
<script src="js/binaryCalculator.js" type="text/javascript"></script> </body>
</html>
Solution:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Buttons</title>
<link rel="stylesheet" href="css/buttonsGrid.css" type="text/css">
</head>
<body>
<div id="res"></div>
<div id="btns">
<button id="btn0" class="btn" onclick="add('0')">0</button>
<button id="btn1" class="btn" onclick="add('1')">1</button>
<button id="btnClr" class="btn" onclick="clearresult()">C</button>
<button id="btnEql" class="btn" onclick="equal()">=</button>
<button id="btnSum" class="btn" onclick="assignOperator('+')">+</button>
<button id="btnSub" class="btn" onclick="assignOperator('-')">-</button>
<button id="btnMul" class="btn" onclick="assignOperator('*')">*</button>
<button id="btnDiv" class="btn" onclick="assignOperator('/')">/</button>
</div>
<script src="js/binaryCalculator.js" type="text/javascript"></script> </body>
</html>
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: lightgreen;
color:white;
}
#btnSum, #btnSub, #btnMul, #btnDiv {
background-color:black;
color:red;
}
.btn {
width: 25%;
height: 36px;
font-size: 18px;
margin: 0px;
float: left;
}
JAVASCRIPT:
const res = document.getElementById("res");
let operand1 = operator = operand2 = '';
function add(value) {
if(operator) {
operand2 += value;
} else {
operand1 += value;
}
renderResult();
}
function clearresult() {
operand1 = operator = operand2 = '';
renderResult();
}
function equal() {
operand1 = parseInt(operand1, 2);
operand2 = parseInt(operand2, 2);
switch(operator) {
case '+':
operand1 += operand2;
break;
case '-':
operand1 -= operand2;
break;
case '*':
operand1 *= operand2;
break;
case '/':
if(operand2)
operand1 /= operand2;
break;
default:
break;
}
operand1 = parseInt(operand1).toString(2);
operator = operand2 = '';
renderResult();
}
function assignOperator(value) {
operator = value;
renderResult();
}
function renderResult() {
res.innerHTML = operand1 + operator + operand2;
}