JS-Math is a library of math functions that allows you to perform common math operations on numbers and arrays.
- First install nodejs https://nodejs.org/
- Verfiy nodejs and npm are installed with commands in the terminal,
node -v
andnpm -v
- Run the command
npm install math
To use JS-Math import the module in your javascript file
const jsMath = require('math');
To run the file, run the command node file_name.js
There are nine basic functions that can be utilized: samesign, copysign, add, sum, mul, prod, factorial, gcd, and lcm.
The samesign function takes two numbers as arguments and returns a boolean whether they have the sign same.
jsMath.samesign(4, 5); //samesign(4, 5) => returns: true
The copy sign function takes two numbers as arguments and returns the first argument with the same sign as the second argument.
jsMath.copysign(-3, 6); //copysign(-3, 6) => returns: 3
The add function takes two numbers as arguments and returns their sum.
jsMath.add(15, 11); //add(15, 11) => returns: 26
The sum function takes an array of numbers as an argument and returns their sum.
jsMath.sum([2,3,4]); //sum([2,3,4]) => returns: 9
The mul function takes two numbers as arguments and returns their product.
jsMath.mul(3,7); //mul(3,7) => returns: 21
The prod function takes an array of numbers as an argument and returns the product of the elements in the array.
jsMath.prod([8,9,23]); //prod([8,9,23]) => returns: 1656
The factorial function takes a number as an argument and returns the factorial.
jsMath.factorial(7); //factorial(7) => returns: 5040
The gcd function takes two numbers as arguments and returns their greatest common divisor.
jsMath.gcd(126, 48); //gcd(126, 48) => returns: 6
The lcm function takes two numbers as arguments and returns their least common mulitiple.
jsMath.lcm(24, 36) //lcm(24, 36) => returns: 72
Additionally, there is a coffeescript file that can be compiled to a javascript file named math.js.
To install coffeescript globally run the command npm install -g coffeescript
To verify coffeescript is installed globally run the command coffee -v
.
To compile the coffeescript file run the command coffee -c math.coffee
in the same directory as the math.coffee file. This will produce a math.js file.
To read more about coffeescript visit https://coffeescript.org/