diff --git a/CONTRIBUTERS.md b/CONTRIBUTERS.md index 50a46667..f1f8ce11 100644 --- a/CONTRIBUTERS.md +++ b/CONTRIBUTERS.md @@ -63,4 +63,4 @@ | Afif Islam | [afif0993](https://github.com/afif0993) | | | Subhadip De | [@subhadipde](https://github.com/subhadipde) | 1 | | Ujjwal Paul | [@ujjwalpaul005](https://github.com/ujjwalpaul005) | 1 | - +| Heet Boda | [heetboda10](https://github.com/heetboda10) | 1 | diff --git a/JavaScript/Binary-Calculator/css/binaryCalculator.css b/JavaScript/Binary-Calculator/css/binaryCalculator.css new file mode 100644 index 00000000..3bca5da7 --- /dev/null +++ b/JavaScript/Binary-Calculator/css/binaryCalculator.css @@ -0,0 +1,115 @@ +html { + overflow: hidden; + background-color: #333; +} + +body { + width: 100%; + height: 100%; +} + +#res { + background-color :lightgray; + border : solid; + height :48px; + font-size :20px; + border-radius: 20px; + padding: 5px 0px 0px 10px; +} + +#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; +} */ + +#btns { + display: block; + height: auto; + padding-top: 10px; +} + +#btns ul { + list-style: none; + display: block; + height: 72px; + margin: 0; + padding: 0; +} + +#btns button { + width: 187px; + height: 65px; + font-size: 18px; + margin: 0; + float: left; + border-radius: 20px; +} + +.container { + margin: auto; + margin-top: 10%; + width: 50%; + height: 50%; + border: 1px solid white; + padding: 10px; + border-radius: 4px; +} + +/* Make it more accessible to smaller screens */ +@media screen and (max-width: 1000px) { + .container { + width: 70%; + height: 70%; + } + + #btns button { + height: 100px; + font-size: 22px; + } + + #btns ul { + height: 100px; + } + + #res { + font-size: 30px; + } +} + +@media screen and (max-width: 700px) { + .container { + width: 95%; + height: auto; + } + + #btns button { + height: 150px; + font-size: 30px; + } + + #btns ul { + height: 150px; + } + + #res { + height: 68px; + font-size: 40px; + } +} diff --git a/JavaScript/Binary-Calculator/index.html b/JavaScript/Binary-Calculator/index.html new file mode 100644 index 00000000..fbe0c14d --- /dev/null +++ b/JavaScript/Binary-Calculator/index.html @@ -0,0 +1,30 @@ + + + + + + Binary Calculator + + + + +
+
+
+ + +
+
+ + + diff --git a/JavaScript/Binary-Calculator/js/binaryCalculator.js b/JavaScript/Binary-Calculator/js/binaryCalculator.js new file mode 100644 index 00000000..c09efb86 --- /dev/null +++ b/JavaScript/Binary-Calculator/js/binaryCalculator.js @@ -0,0 +1,81 @@ +var res = document.getElementById("res"); + +function clc(){ + res.innerHTML = ""; +} + +function insert(num){ + res.innerHTML += num; +} + +function eql(){ + let result = res.innerHTML; + + operator = result.replace(/[0-9]/g, ''); + result = result.split(new RegExp(/[+\-*/]/)); + + let num1 = result[0]; + let num2 = result[1]; + + num1 = binaryToDecimal(num1); + num2 = binaryToDecimal(num2); + + switch(operator){ + case '+': + value = num1 + num2; + break; + case '*': + value = num1 * num2; + break; + case '/': + value = num1 / num2; + break; + case '-': + value = num1 - num2; + break; + default: + value = '' + break; + } + res.innerHTML = decimalToBinary(value); +} + +function decimalToBinary(num){ + let binary = []; + while(num > 0){ + binary.push(num % 2); + num = Math.floor(num / 2); + } + return binary.reverse().join(''); +} + +function binaryToDecimal(binary){ + let decimal = 0; + for(let i = 0; i < binary.length; i++){ + decimal += binary[i] * Math.pow(2, binary.length - i - 1); + } + return decimal; +} + +// register a keystroke listener +document.addEventListener('keydown', (event) => { + if (event.which == 96) { // if numpad0 + insert(0); + } else if (event.which == 97) { // if numpad1 + insert(1); + } else if (event.which == 48) { // if digit0 + insert(0); + } else if (event.which == 49) { // if digit1 + insert(1); + } else if (event.which == 107) { // if numpadAdd + insert('+'); + } else if (event.which == 109) { // if numpadSubtract + insert('-'); + } else if (event.which == 111) { // if numpadDivide + insert('/'); + } else if (event.which == 106) { // if numpadMultiply + insert('*'); + } else if (event.which == 13) { // if numpadEnter + eql(); + } +});