math-expression-generator
JavaScript library for generating mathematical expressions that evaluate to a particular number
Last updated a year ago by bent0b0x .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install math-expression-generator 
SYNC missed versions from official npm registry.

Math Expression Generator

CircleCI

JavaScript library to compute random arithmetic expressions (of configurable length) that evalute to a given number.

Usage

import { generateExpression } from "math-expression-generator";

const expression = generateExpression({
    target: 20,
    length: 2
});

console.log(expression); // [15, '+', 5]

const longExpression = generateExpression({
    target: 4828,
    length: 5
});

console.log(longExpression); // [ 2, '*', 13, '*', 10, '*', 1207, '/', 65 ]

Evaluate the expression

If you want to evaluate the returned expression (to verify it is correct, for example), you can use mathjs (but there are plenty of other library solutions out there):

import { generateExpression } from "math-expression-generator";
import math from "mathjs";

const expression = generateExpression({
    target: 20,
    length: 2
});

const result = math.eval(expression.join(" "));

console.log(result); // 20

Documentation

Generated with typedoc ❤️

Click to view docs

TypeScript Types

If you are using TypeScript in your own project, you can import some types from this library.

Expression: import Expression from 'math-expression-generator/types/Expression';

Expression docs

Operator: import Operator from 'math-expression-generator/types/Operator';

Operator docs

Current Tags

  • 1.3.0                                ...           latest (a year ago)

7 Versions

  • 1.3.0                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (20)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |