MathJs is a small javascript library for performing mundane tasks repeatedly
in a easy way. A few of the things that this library is capable of are operations like,
round
ing a decimal upto 2 decimal places, or trying to find the difference
between
two arrays.
Read on to find out more features about this library.
You can download this library as an npm module by running the following command
npm install simplemathjs
Once downloaded, you can include in your html file like so
<script type="text/javascript" src="node_modules/dist/math.min.js"></script>
That's it, you can now use this library by referring to the MJ
variable
within your javascript
MJ.round(3.14159265, 2)
// 3.14
MJ.arrayDiff([1, 2, 3, 4], [1, 4, 2])
// [3]
There functionalities are segrated by primarily three sections
- Array Functionalities
- Numeric Functionalities
- String Functionalities
List of Functions that you can operate on Arrays
Given an array of arrays, it will flatten hierarchies of arrays and return a single array.
MJ.arrayFlatten([1, 2, [3, 4, [5, 6], [7]], [8, 9]])
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
Given a list of items in an array, it will returns an array of elements containing all the elements occurring only once, i.e. removes duplicates.
MJ.arrayUnique([1, 1, 'b', false, false, true, 'b', undefined, undefined])
// [1, "b", false, true, undefined]
Given two sets of arrays, returns the set of unique elements which are present in both
MJ.arrayIntersection([1, 2, 3, 2], [1, 5, 2, 1])
// [1, 2]
Given two set of arrays, returns the set of elements present in the left, but not in the right.
MJ.arrayDiff([1, 2, 3, 4], [1, 4, 2])
// [3]
Given a set of arrays as arguments, returns a single array containing all the elements present in every argument, including duplicates.
NOTE: the arguments for the function always need to be an array
MJ.arrayUnion([1], [], [2])
// [1, 2]
Given an array of numbers, computes the mean and returns it.
MJ.arrayMean([1, 2, 3, 100, 101, 202])
// 68.16666666666667
List of Functions that you can operate on Numbers
Given a long
type number, you can round it off to how many ever
decimal places you want to
MJ.round(3.14159265, 3)
// 3.141
Returns the factorial for a number
MJ.factorial(5)
// 120
Get the prime factors for a number
MJ.factors(10)
// [1, 10, 2, 5]
Checks whether a given number is prime or not
MJ.isPrime(5)
// true
Returns the remainder when a numerator is divided by a denominator
MJ.remainder(10, 4)
// 2
Returns the exponent value of a number.
MJ.power(2, 5)
// 32
// you can also directly get the `square` or the `cube`
MJ.square(2)
// 4
MJ.cube(2)
// 8
List of Functions that you can operate on Strings
Convert a string to titlecase.
MJ.titleCase(' abC dEf')
// Abc Def