Skip to content

Added name and my JS project Binary-Calculator #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
115 changes: 115 additions & 0 deletions JavaScript/Binary-Calculator/css/binaryCalculator.css
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 30 additions & 0 deletions JavaScript/Binary-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!-- Enter your HTML code here -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Binary Calculator</title>
<link rel="stylesheet" href="css/binaryCalculator.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<div id="res"></div>
<div id="btns">
<ul>
<li><button onclick="insert(0)" id="btn0" style="margin-right: 6px;">0</button></li>
<li><button onclick="insert(1)" id="btn1" style="margin-right: 6px;">1</button></li>
<li><button onclick="clc()" id="btnClr" style="margin-right: 6px;">C</button></li>
<li><button onclick="eql()" id="btnEql">=</button></li>
</ul>
<ul>
<li><button onclick="insert('+')" id="btnSum" style="margin-right: 6px;">+</button></li>
<li><button onclick="insert('-')" id="btnSub" style="margin-right: 6px;">-</button></li>
<li><button onclick="insert('*')" id="btnMul" style="margin-right: 6px;">*</button></li>
<li><button onclick="insert('/')" id="btnDiv">/</button></li>
</ul>
</div>
</div>
<script src="js/binaryCalculator.js" type="text/javascript"></script>
</body>
</html>
81 changes: 81 additions & 0 deletions JavaScript/Binary-Calculator/js/binaryCalculator.js
Original file line number Diff line number Diff line change
@@ -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();
}
});