Skip to content

Latest commit

 

History

History
222 lines (158 loc) · 6.44 KB

README.md

File metadata and controls

222 lines (158 loc) · 6.44 KB

🖩 Calculator

This project is a simple calculator implemented with JavasScript

GitHub language count Repository size License GitHub last commit

image


📌 Table of Contents


💡 Features

  • ➕ Do simple math calculations
  • 🗑 Delete the last value
  • 🗑 Delete all expression
  • 📋 Copy the result

🎯 Purpose

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

🛠 Installation

You need to download git initially

Run this command to clone the repository:


git clone https://github.com/AleNoia/calculator.git


📝 Utilization

It is very simple to use this calculator

To start the calculator

// ======================================== [START CALCULATOR]
    this.start = () => {
        this.display.focus();
        this.getClicks();
        this.getEnter();
        this.getDel();
    }

Getting the clicks and applying to a function

// ======================================== [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();
    })
}

Getting enter to calculate

// ==================== [GET ENTER TO CALCULATE]
    this.getEnter = () => {
        document.addEventListener('keyup', e => {
            if (e.keyCode === 13) this.calculate();
        })
    }

Getting delete button to delete last value

// ==================== [GET DEL TO DELETE LAST VALUE]
this.getDel = () => {
    document.addEventListener('keypress', e => {
        if (e.keyCode === 8) this.del();
    })
}

Calculating

// ==================== [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;
        }
    }

You can delete the last value

image

// ==================== [DELETE THE LAST CARACTER ON DISPLAY]
this.del = () => this.display.value = this.display.value.slice(0, -1)

Clear display

image

// ==================== [CLEAR DISPLAY]
this.clear = () => this.display.value = '';

Copy the result

image

// ==================== [COPY]
this.copyResult = () => {
    let copyValue = this.display
    copyValue.select();
    document.execCommand('copy');
}

⚙ Technologies used

Technologies that were used in the construction of the project:


🤝 Contributing

  1. Fork the project.
  2. Create a new branch with your changes: git checkout -b my-feature
  3. Save your changes and create a commit message telling you what you did: git commit -m" feature: My new feature "
  4. Submit your changes: git push origin my-feature
  5. Now just open your pull request in the repository that you forked describing your changes
  6. 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 🙂


👋 Author

If you want to contact, mail me or send a message on Twitter

Gmail Badge badge


🧾 License

Released in 2021. This project is under the MIT license.

Made by Igor Noia 👋