Coder Social home page Coder Social logo

calculator's Introduction

Kevin's Calculator

Vanilla Javascript App


This is a simple calculator with a running total and memory options


Try it here


Summary

This calculator app is a great example of something that seems like it should be really simple but it's far from it. Not only is there quite a bit of work involved just to make it look like a calculator on the screen, mathematical order of operations threw me for a loop. Trying to get a running tally while having to follow proper order of operations proved to be a little more complicated than I thought.

The best part about building this app, that took me about 1 1/2 weeks, is that it forced me to spend a lot of time in the planning and whiteboarding stage as opposed to jumping in too quickly. I was forced to come up with an algorithm to figure out how to manage the calculations and then choose a data structure that best fit it.

I'm really happy with how this turned out from both a visual user design to the functionality working well.

My Algorithm

Since there are no parenthesis or exponents on this calculator I found that there would never need to be more than 6 items involved in a calculation at a time. When calculating a running total, the first two numbers can only ever be calculated immediately if they are being multiplied or divided. If they are being added together or subtracted we would need to see what the next operator would be. This is due to the potential for the user to multiplying the second two numbers entered. For example: 2 + 3 x 2 = 8, not 10, due to the order of operations.

When I saw this pattern I realized I could use a simple array and just calculate as needed within the array. Once I had this algorithm I was able to write the actual code fairly quickly (minus a few bugs here and there I needed to resolve).

Author

calculator's People

Contributors

kbiz65 avatar

Watchers

James Cloos avatar  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.