console
var btn0 = document.getElementById("btn0");
var btn1 = document.getElementById("btn1");
var btnEql = document.getElementById("btnEql");
var btnClr = document.getElementById("btnClr");
var a, b, operation;
btn0.onclick = function () {
res.innerHTML += "0";
};
btn1.onclick = function () {
res.innerHTML += "1";
};
btnClr.onclick = function () {
res.innerHTML = "";
};
btnEql.onclick = function () {
b = res.innerHTML;
const c = parseInt(a, 2);
const d = parseInt(b, 2);
res.innerHTML = eval(`(${c} ${operation} ${d}).toString(2)`);
};
function action(e) {
var btn = e.target || e.srcElement;
a = res.innerHTML;
operation = btn.innerHTML;
res.innerHTML = "";
}
document.getElementById("btnSum").onclick = action;
document.getElementById("btnSub").onclick = action;
document.getElementById("btnMul").onclick = action;
document.getElementById("btnDiv").onclick = action;
<h1>binary calculator</h1>
<div id="container">
<div id='res'></div>
<div id='btns'>
<button id='btn0'>0</button>
<button id='btn1'>1</button>
<button id='btnClr'>C</button>
<button id='btnEql'>=</button>
<button id='btnSum'>+</button>
<button id='btnSub'>-</button>
<button id='btnMul'>*</button>
<button id='btnDiv'>/</button>
</div>
</div>
<p class="description">
simple calculator that performs the following <span>operations on binary numbers</span>:<br> addition, subtraction, multiplication, and division.
</p>
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;900&display=swap");
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
user-select: none;
}
html,
body {
width: 100%;
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
background-color: #f5f5f5;
font: 400 14px Roboto, sans-serif;
}
h1 {
margin-bottom: 30px;
font-size: 28px;
color: #335c62;
}
#container {
width: 33%;
}
#res {
background-color: lightgray;
height: 48px;
font-size: 26px;
border: 1px solid #f5f5f5;
border-radius: 5px;
padding: 5px 20px;
color: #363636;
}
#btns button {
width: 25%;
height: 36px;
font-size: 20px;
margin: 0px;
float: left;
border: 2px solid #f5f5f5;
border-radius: 5px;
}
#btn0,
#btn1 {
background-color: #59a2ad;
color: #f3f3f3;
}
#btnClr,
#btnEql {
background-color: #335c62;
color: #f3f3f3;
}
#btnSum,
#btnSub,
#btnMul,
#btnDiv {
background-color: #363636;
color: #f3f3f3;
}
.description {
margin-top: 50px;
text-align: center;
color: #999;
width: 40%;
line-height: 28px;
font-size: 16px;
}
span {
color: #335c62;
}