Created
March 5, 2023 08:25
-
-
Save ShaakhDev/65a71c3b88bf8b0e7eb81785f6b5ecd2 to your computer and use it in GitHub Desktop.
hackerrang binary calculator solution.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const display = document.getElementById('res'); | |
const btns = document.getElementById('btns') | |
let inputValue = ""; | |
let regexForExeptSigns = /[+\-*/]/; | |
let operation = ""; | |
btns.addEventListener('click',handler) | |
function handler(event) { | |
let sign = event.target.innerText; | |
if (sign === '0' || sign === '1') inputValue += sign; | |
if ((sign === '+' || | |
sign === '-' || | |
sign === '*' || | |
sign === '/')&& | |
!inputValue.match(regexForExeptSigns) | |
){ | |
inputValue += sign; | |
operation = sign; | |
} | |
if(sign === 'C') inputValue = ""; | |
if(sign === '=') calculate() | |
displayInput(); | |
} | |
//calculating the result | |
function calculate(){ | |
let [firstBin,secondBin] = inputValue.split(regexForExeptSigns); | |
let result = getResult(firstBin,secondBin).toString(2) | |
inputValue = result; | |
}; | |
function getResult(first,second){ | |
if(operation === '+') return BigInt('0b' + first) + BigInt('0b' + second); | |
if(operation === '-') return BigInt('0b' + first) - BigInt('0b' + second); | |
if(operation === '*') return BigInt('0b' + first) * BigInt('0b' + second); | |
if(operation === '/') return BigInt('0b' + first) / BigInt('0b' + second); | |
} | |
//displaying an input string | |
function displayInput(){ | |
display.innerText = inputValue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment