Skip to content
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

completed-100devs-calculator #391

Open
wants to merge 2 commits into
base: main
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
40 changes: 40 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="calculator">
<div class="display-container">
<div class="display">
<span class="result">0</span>
</div>
</div>

<div class="buttons">
<div class="button">7</div>
<div class="button">8</div>
<div class="button">9</div>
<div class="button">/</div>
<div class="button">4</div>
<div class="button">5</div>
<div class="button">6</div>
<div class="button">X</div>
<div class="button">1</div>
<div class="button">2</div>
<div class="button">3</div>
<div class="button">+</div>
<div class="button">0</div>
<div class="button">.</div>
<div class="button">=</div>
<div class="button">-</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
60 changes: 60 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class Calculator {
operator;
a = 0;
b = 0;
res = 0;
calculated = false;

handleClick = (btn) => {
const ops = ["+", "-", "X", "/"];
const val = btn.srcElement.innerText;
if (val === "=") this.calculate();
else if (ops.includes(val)) this.setOperator(val);
else if (this.operator === undefined) this.setA(val);
else this.setB(val);
}

updateResult = (val) => {
const result = document.querySelector(".result");
result.innerText = val;
}

setA(num) {
if (this.a === 0) this.a = num;
else if (num === "." && this.a.includes(".")) return;
else this.a += num;
this.updateResult(this.a);
}

setB(num) {
if (this.b === 0) this.b = num;
else if (num === "." && this.b.includes(".")) return;
else this.b += num;
this.updateResult(this.b);
}

setOperator(str) {
this.operator = str;
this.updateResult(this.operator);
}

calculate = () => {
if(this.operator == "+") this.res = Number(this.a) + Number(this.b);
else if(this.operator == "-") this.res = Number(this.a) - Number(this.b);
else if(this.operator == "X") this.res = Number(this.a) * Number(this.b);
else if(this.operator == "/") this.res = Number(this.a) / Number(this.b);

this.operator = undefined;
this.a = 0;
this.b = 0;
this.updateResult(this.res);
}

}

const cal = new Calculator;

const buttons = document.querySelectorAll(".button");
buttons.forEach(btn => {
btn.addEventListener("click", cal.handleClick);
});
56 changes: 56 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
* {
font-size: 62.5%;
}

.container {
height: 100vh;
display: flex;
justify-content: center;
}

.calculator {
margin-top: 15%;
height: 575px;
width: 450px;
background: black;
border-radius: 5%;
}

.display-container {
display: flex;
justify-content: center;
}

.display {
margin-top: 20%;
height: 100px;
width: 80%;
background: white;
display: flex;
justify-content: flex-end;
align-items: center;
}

.display > span {
font-size: 7rem;
}

.buttons {
display: flex;
flex-wrap: wrap;
margin: 25px 15% 0 15%;
max-width: 100%;
justify-content: center;
align-content: center;
}

.button {
background: white;
height: 65px;
border-radius: 50%;
margin: 5px;
flex: 1 20%;
font-size: 5rem;
text-align: center;
cursor: pointer;
}