Computes random arithmetic expressions (of configurable length) that evalute to a given number.
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 ]
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);
console.log(result); // 20
Generated using TypeDoc