This mini JavaScript library is made up of useful mathematical functions that the inbuilt Math library does not have.
Accepts an integer and returns the product of all the integers up to and including the integer.
Maths.factorial(4) === 24 //4 x 3 x 2 x 1
Takes two parameters min
and max
and returns a random integer between the two numbers.
Maths.randomInt(2,7) = 2
Maths.randomInt(2,7) = 4
Maths.randomInt(6) = 3 //random integer between 0 and 5
Accepts an integer and returns a Boolean value based on whether it is a Prime number or not. Prime numbers are numbers that only have two distinct factors.
Maths.isPrime(13) === true
Maths.isPrime(20) === false
Accepts an integer and returns an array of all its factors in ascending order.
Maths.factors(20) === [1,2,4,5,10,20]
Accepts an integer and returns a Boolean value based on whether it is an even number.
Math.even(2) === true
Maths.even(5) === false
Accepts an integer and returns a Boolean value based on whether it is an odd number
Maths.odd(3) === true
Maths.odd(8) === false
Accepts an integer (n) and returns the sum of all the numbers up to and including the number. It essentially calculates the nth Triangle Number.
Maths.sum(6) === 21 //1 + 2 + 3 + 4 + 5 + 6
Accepts an integer (n) and returns the sum of the first n square numbers.
Maths.sumOfSquares(3) === 14 // 1 + 4 + 9
Accepts an integer (n) and returns the sum of the first n cube numbers.
Maths.sumOfCubes(4) === 100 //1 + 8 + 27 + 64
Accepts an integer and returns the sum of all the individual digits until the value is a single integer.
Maths.digitSum(123) === 6 //1 + 2 + 3
Takes two parameters, a floating-point number and the number of decimal places wanted (with a default value of 0). Returns the floating-point number rounded to the specified number of decimal places.
Maths.roundDP(1.234567, 3) === 1.235
Maths.roundDP(3.14) === 3
Takes two parameters, a real number and the number of figures wanted (with a default value of 1). Returns the real number rounded to the specified number of figures.
Maths.roundSF(1234567, 3) === 1230000
Math.roundSF(123.45) === 100
Takes two integers as parameters and returns the greatest common divisor of the two.
Maths.gcd(20, 10) === 10
Takes two integers as parameters and returns the lowest common multiple of the two.
Maths.lcm(2,3) === 6
Maths.lcm(8,4) === 8