Coder Social home page Coder Social logo

kelvynvalle / matheval Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 6 KB

Math evaluator

HTML 4.84% JavaScript 95.16%
expression expression-eval expression-evaluator expression-parser expression-parsing javascript math math-eval math-evaluator math-parser

matheval's Introduction

matheval.js


Javascript arithmetic expression evaluator


๐Ÿ“šโœ–๏ธโž•โž—๐Ÿ”ข


๐Ÿค“ Matheval? What?

Yes. Matheval. This is a small javascript project focused in allow you and me to safely evaluate arithmetic expressions in strings, returning a float as result. Yes, without the ill-spoken Javascript's eval.

๐Ÿค” But... Why would I use it?

Mmmm... I don't know. But if you need to evaluate an arithmetic expression from a string, certainly it will be useful. With Matheval.js you can interpret math expressions in strings easily and safely, in a free and open source approach.

๐Ÿ“ How can I implement it?

Step 1: Download mathEval.js

Did you download? So, just move the file to your project's directory.

Step 2: Paste the following in the header of your page:

<script src="mathEval.js"></script>

Step 3: It's done!

To use the mathEval() function, just call it with the string expression as parameter.

๐Ÿ“ Code examples

String:

mathEval("1 + 1 + 1"); //returns 3
mathEval("2 * 7"); //returns 14
mathEval("8 * 2 - 1"); //returns 15
mathEval("2 ^ 7 + 110 - (2 * 73)"); //returns 92
mathEval("600 / 10 + (2.5 * 2)"); //returns 65

Using template strings:

let a = 15;
let b = 20;
let result = mathEval(`${a} + ${b}`); //returns 35

Concatenating:

let a = 90;
let b = 1;
let result = mathEval(a + " - " + b); //returns 89

Getting expressions from users:

//user-input is the id of the input text that get expression from user
var expression = document.getElementById("user-input").value;
let result = mathEval(expression);

Code generated expression:

//myExpression() is a function that returns an string expression
var expression = myExpression();
let result = mathEval(expression);

matheval's People

Stargazers

 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.