Giter Club home page Giter Club logo

javascript-calculator's Introduction

JavaScript-Calculator

Live Link: https://rraj13.github.io/JavaScript-Calculator/

alt text

Introduction

Hey there! Here I have created a four function calculator using JavaScript and a bit of JQuery for DOM Manipulation. The user enters the operation they wish to perform, hits the "=" button and are presented with the calculated result. The program can handle both single and multiple operation expressions, following standard order of operations rules.

I took on this project to test my ability to handle advanced array manipulation and looping. Incorporating order of operations capabilities proved much more difficult than anticipated. But, after a good amount of thinking, research and some trial and error, I was eventually able to code it! Please see below for more info on the specific logic used under "General Methodology."

Technologies

HTML 5
CSS 3
JavaScript
JQuery

General Methodology

The main methodology I implemented in this project was array concatenation, parsing and looping. When the user clicks on a number or operation, all the corresponsing values are pushed to an array called expressionArr.

For example,

If a user enters 25 + 2 / 3 ==> ["2", "5", "+". "2". "/", "3"] is now the new expressionArr.

The next step was take loop through the array and ensure that multiple digit numbers are accounted for. Here, I used some logic to essentially say that anytime an array element is followed by a number, combine them, and add the new multiple digit number to the array, replacing the original two.

From here, the array would become ["25", "+", "2", "/", "3"], and I would then loop through the array once more and parse all the strings that included numbers to numbers and push them to an array called calculationArr.

So, calculationArr = [25, "+", 2, "/", 3].

Now here was the difficult part: Order of Operations.

Order of operations states the following:

  1. Do multiplication/division from left to right, then
  2. Do addition/subtraction from left to right.

In order to achieve this, I looped through the calculationArr yet again and anytime a multiplication or division symbol was found, I would perform the respective operation on the number before and after the sign and replace it in the array. The same was then performed for addition/subtraction but strictly done after mutiplication/division were complete.

Once all the operations were complete, the calculationArr should have only one number, and that would be the result.

Well, that was a lot. Please feel free to take a look at my code and reach out with any questions!

Credits

Apple iOS Calculator Design
SchemeColor article - https://www.schemecolor.com/apple-calculator-app-icon-2017-colors.php

javascript-calculator's People

Contributors

rraj13 avatar rahulrajdev avatar

Stargazers

 avatar Dan Blaker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.