Add tests to Math (#423)

* Add prettier config

* test: add test to check for absolute function

* chore: es5 to es6

* test: add test to check mean function

* test: add test for sum of digit

* test: add test for factorial

* test: add test for fibonnaci

* test: add test for find HCF

* test: add test for lcm

* test: add gridget test

* test: add test for mean square error

* test: add test for modular binary exponentiation

* test: add tests for palindrome

* test: add test for pascals triangle

* test: add tests for polynomial

* test: add tests for prime check

* test: add tests for reverse polish notation

* test: add tests for sieve of eratosthenes

* test: add tests for pi estimation monte carlo method

* chore: move tests to test folder

* chore: fix standardjs errors
This commit is contained in:
Ephraim Atta-Duncan
2020-10-11 19:47:49 +00:00
committed by GitHub
parent 554abf7126
commit e112434dee
41 changed files with 420 additions and 172 deletions

View File

@@ -1,4 +1,3 @@
/**
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
* Terms of a polynomial are:
@@ -20,18 +19,19 @@ class Polynomial {
* Function to construct the polynomial in terms of x using the coefficientArray
*/
construct () {
this.polynomial = this.coefficientArray.map((coefficient, exponent) => {
if (coefficient === 0) {
return '0'
}
if (exponent === 0) {
return `(${coefficient})`
} else if (exponent === 1) {
return `(${coefficient}x)`
} else {
return `(${coefficient}x^${exponent})`
}
})
this.polynomial = this.coefficientArray
.map((coefficient, exponent) => {
if (coefficient === 0) {
return '0'
}
if (exponent === 0) {
return `(${coefficient})`
} else if (exponent === 1) {
return `(${coefficient}x)`
} else {
return `(${coefficient}x^${exponent})`
}
})
.filter((x) => {
if (x !== '0') {
return x
@@ -55,28 +55,9 @@ class Polynomial {
*/
evaluate (value) {
return this.coefficientArray.reduce((result, coefficient, exponent) => {
return result + coefficient * (Math.pow(value, exponent))
return result + coefficient * Math.pow(value, exponent)
}, 0)
}
}
/**
* Function to perform tests
*/
const tests = () => {
const polynomialOne = new Polynomial([1, 2, 3, 4])
console.log('Test 1: [1,2,3,4]')
console.log('Display Polynomial ', polynomialOne.display())
// (4x^3) + (3x^2) + (2x) + (1)
console.log('Evaluate Polynomial value=2 ', polynomialOne.evaluate(2))
// 49
const polynomialTwo = new Polynomial([5, 0, 0, -4, 3])
console.log('Test 2: [5,0,0,-4,3]')
console.log('Display Polynomial ', polynomialTwo.display())
// (3x^4) + (-4x^3) + (5)
console.log('Evaluate Polynomial value=1 ', polynomialTwo.evaluate(1))
// 4
}
tests()
export { Polynomial }