Code Monkey home page Code Monkey logo

js-calculator's Introduction

Javascript Calculator

Code to tokenize and interpret mathematical expressions similarily to a default calculator app.

Explaining the pipeline

For the sake of education, I wanted to write up the reasons why the code is structured as it is. While the parts are separate, and you can read only the headings that interest you, I do want to recommend to stick for the full readme. It might give you some better insight.

Input

There's not too much to say from the input. Any string of text will work. However your text needs to be a proper mathematical expression. e.g. 1+1 is valid, but, hello+3 is not. This is because identifiers (e.g. variables) are not supported as of the time of writing this. Neither are multiple expressions.

Tokenizing

Assuming an expression from a single string does not make much sense, to solve this: we categorize each part of the expression. For example 1+1 becomes Number(1) Add Number(1). While interpreting it is a lot easier to go over categories of tokens over an array of characters. This is because we delete any unneeded information. (e.g. spaces, tabs)

Interpreting

The pros of trying to intrepret mathematical expressions is that we already have a rulebook set in place. we know Multiplication/Division happen before Addition/Subtraction, and we need to take this into account when we create results.

How do we do that? We can use checks when we read a number or expression to see what is going to happen with that number in the long term. An example would be 1 + 1 * 2. To read this expression the following steps occur.

Perform Addition:
 - Read Number (1)     [next: Add]
   - Is the next operation happening multiplication/division? No
   - Is the operation happening addition/subtraction? Yes
   - Return (1)
 - Read Operator (Add) [next: Number(1)]
 - Read Number (1)     [next: Multiply]
   - Is the next operation happening multiplication/division? Yes
   - Perform Multiplication(1 * 2)
   - Return (2)
 - Return(3)

js-calculator's People

Contributors

velddev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.