This project is a simple calculator implemented with JavasScript
- π‘ Features
- π― Purpose
- π Installation
- π Utilization
- π€ Contributing
- β Technologies Used
- π Author
- π§Ύ License
- β Do simple math calculations
- π Delete the last value
- π Delete all expression
- π Copy the result
My purpose with this project is learn more about Javascript and how to do a calculator.
During this project I learned specially:
- How to do a calculator
- More about how to use mathematical properties in Javascript
- More about SCSS
- How to use CSS Grid
- And so on
You need to download git initially
Run this command to clone the repository:
git clone https://github.com/AleNoia/calculator.git
It is very simple to use this calculator
// ======================================== [START CALCULATOR]
this.start = () => {
this.display.focus();
this.getClicks();
this.getEnter();
this.getDel();
}
// ======================================== [GET THE CLICKS AND APPLIES A FUNCTION]
this.getClicks = () => {
document.addEventListener('click', e => {
const el = e.target;
if (el.classList.contains('btn-calculate')) this.calculate();
if (el.classList.contains('btn-num')) this.addNumDisplay(el);
if (el.classList.contains('btn-percent')) this.percent();
if (el.classList.contains('btn-potentiation')) this.potentiation();
if (el.classList.contains('btn-squareRoot')) this.squareRoot();
if (el.classList.contains('btn-changeSignal')) this.changeSignal();
if (el.classList.contains('btn-clear')) this.clear();
if (el.classList.contains('btn-del')) this.del();
if (el.classList.contains('btn-copy')) this.copyResult();
})
}
// ==================== [GET ENTER TO CALCULATE]
this.getEnter = () => {
document.addEventListener('keyup', e => {
if (e.keyCode === 13) this.calculate();
})
}
// ==================== [GET DEL TO DELETE LAST VALUE]
this.getDel = () => {
document.addEventListener('keypress', e => {
if (e.keyCode === 8) this.del();
})
}
// ==================== [CALCULATE]
this.calculate = () => {
try {
let valueDisplay = this.display.value
// ==================== [CALCULATE]
expressionResult = new Function("return " + valueDisplay);
result = expressionResult(); // Result
this.getLastResult(valueDisplay, result);
this.display.value = result
} catch (e) {
alert("That is not an expression");
this.display.value = '';
return;
}
}
// ==================== [DELETE THE LAST CARACTER ON DISPLAY]
this.del = () => this.display.value = this.display.value.slice(0, -1)
// ==================== [CLEAR DISPLAY]
this.clear = () => this.display.value = '';
// ==================== [COPY]
this.copyResult = () => {
let copyValue = this.display
copyValue.select();
document.execCommand('copy');
}
Technologies that were used in the construction of the project:
- Fork the project.
- Create a new branch with your changes:
git checkout -b my-feature
- Save your changes and create a commit message telling you what you did:
git commit -m" feature: My new feature "
- Submit your changes:
git push origin my-feature
- Now just open your pull request in the repository that you forked describing your changes
- After the merge of your pull request is done, you can delete yout branch
If you have any questions check this guide on how to contribute
Feel free to contribute π
If you want to contact, mail me or send a message on Twitter
Released in 2021. This project is under the MIT license.
Made by Igor Noia π