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

Answer #365

Open
wants to merge 6 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
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.0">
<title>100Devs Calculator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main class="calculator">
<section class="main__display">
<label for="display"></label>
<input id="display" type="text" name="display" value="" readonly>
</section>
<section class="buttons__container">
<div class="buttons">
<button class="btn expr">7</button>
<button class="btn expr">8</button>
<button class="btn expr">9</button>
<button class="btn expr">/</button>
<button class="btn expr">4</button>
<button class="btn expr">5</button>
<button class="btn expr">6</button>
<button class="btn expr">x</button>
<button class="btn expr">1</button>
<button class="btn expr">2</button>
<button class="btn expr">3</button>
<button class="btn expr">+</button>
<button class="btn expr">0</button>
<button class="btn expr">.</button>
<button class="btn equal">=</button>
<button class="btn expr">-</button>
</div>
</section>
</main>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/12.0.0/math.js"></script>

<script type="module" src="index.js"></script>
</body>
</html>
55 changes: 55 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class Calculator {
constructor() {
this.evalStr = '';
this.result = '';
this.calculator = math;
}

checkForX(expr) {
return expr.includes('x') ? expr.replace('x', '*') : expr;
}

evaluate(expr) {
let ex = this.checkForX(expr);
try {
this.result = this.calculator.evaluate(ex);
return this.result;
} catch (error) {
return 'Error in expression!'
}
}

clear() {
this.evalStr = '';
this.result = '';
}

get getEvalStr() {
return this.evalStr;
}

set setEvalStr(expression) {
this.evalStr = expression
}
}

let myCalculator = new Calculator()

const display = document.getElementById('display');
const btns = document.querySelectorAll('.expr');
const eqBtn = document.querySelector('.equal');

btns.forEach(btn => {
btn.addEventListener('click', (btn) => run(btn));
})

eqBtn.addEventListener('click', solve)

function run(btn) {
display.value += btn.target.innerText;
}

function solve() {
let answer = myCalculator.evaluate(display.value);
display.value = answer;
}
1 change: 1 addition & 0 deletions node_modules/.bin/mathjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions node_modules/@babel/runtime/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions node_modules/@babel/runtime/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions node_modules/@babel/runtime/helpers/AsyncGenerator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node_modules/@babel/runtime/helpers/AwaitValue.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions node_modules/@babel/runtime/helpers/OverloadYield.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions node_modules/@babel/runtime/helpers/applyDecoratedDescriptor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading