From e112434dee038b4605fe7bbd4e3e7b5b5e322073 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan <55143799+dephraiim@users.noreply.github.com> Date: Sun, 11 Oct 2020 19:47:49 +0000 Subject: [PATCH] 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 --- .prettierrc | 15 ++++++ Maths/Abs.js | 6 +-- Maths/AverageMean.js | 8 ++- Maths/{digitSum.js => DigitSum.js} | 8 ++- Maths/Factorial.js | 19 +++---- Maths/Fibonacci.js | 18 ++----- Maths/FindHcf.js | 5 +- Maths/FindLcm.js | 25 ++++++---- Maths/GridGet.js | 11 ++--- Maths/MeanSquareError.js | 7 +-- Maths/ModularBinaryExponentiationRecursive.js | 11 +---- Maths/Palindrome.js | 9 ++-- Maths/PascalTriangle.js | 27 +++++----- Maths/PiApproximationMonteCarlo.js | 8 +-- Maths/Polynomial.js | 49 ++++++------------- Maths/PrimeCheck.js | 14 ++---- Maths/ReversePolishNotation.js | 8 ++- Maths/SieveOfEratosthenes.js | 22 +++------ Maths/test/Abs.test.js | 13 +++++ Maths/test/AverageMean.test.js | 11 +++++ Maths/test/DigitSum.test.js | 11 +++++ Maths/test/Factorial.test.js | 35 +++++++++++++ Maths/test/Fibonacci.test.js | 30 ++++++++++++ Maths/test/FindHcf.test.js | 20 ++++++++ Maths/test/FindLcm.test.js | 20 ++++++++ Maths/test/GridGet.test.js | 16 ++++++ Maths/test/MeanSquareError.test.js | 21 ++++++++ ...dularBinaryExponentiationRecursive.test.js | 7 +++ Maths/test/Palindrome.test.js | 16 ++++++ Maths/test/PascalTriangle.test.js | 20 ++++++++ Maths/test/PiApproximationMonteCarlo.test.js | 9 ++++ Maths/test/Polynomial.test.js | 37 ++++++++++++++ Maths/test/PrimeCheck.test.js | 14 ++++++ Maths/test/ReversePolishNotation.test.js | 11 +++++ Maths/test/SieveOfEratosthenes.test.js | 14 ++++++ String/PatternMatching.js | 1 - String/{ => test}/CheckAnagram.test.js | 4 +- String/{ => test}/CheckPalindrome.test.js | 2 +- String/{ => test}/PatternMatching.test.js | 6 ++- String/{ => test}/ReverseString.test.js | 2 +- String/{ => test}/ReverseWords.test.js | 2 +- 41 files changed, 420 insertions(+), 172 deletions(-) create mode 100644 .prettierrc rename Maths/{digitSum.js => DigitSum.js} (70%) create mode 100644 Maths/test/Abs.test.js create mode 100644 Maths/test/AverageMean.test.js create mode 100644 Maths/test/DigitSum.test.js create mode 100644 Maths/test/Factorial.test.js create mode 100644 Maths/test/Fibonacci.test.js create mode 100644 Maths/test/FindHcf.test.js create mode 100644 Maths/test/FindLcm.test.js create mode 100644 Maths/test/GridGet.test.js create mode 100644 Maths/test/MeanSquareError.test.js create mode 100644 Maths/test/ModularBinaryExponentiationRecursive.test.js create mode 100644 Maths/test/Palindrome.test.js create mode 100644 Maths/test/PascalTriangle.test.js create mode 100644 Maths/test/PiApproximationMonteCarlo.test.js create mode 100644 Maths/test/Polynomial.test.js create mode 100644 Maths/test/PrimeCheck.test.js create mode 100644 Maths/test/ReversePolishNotation.test.js create mode 100644 Maths/test/SieveOfEratosthenes.test.js rename String/{ => test}/CheckAnagram.test.js (92%) rename String/{ => test}/CheckPalindrome.test.js (90%) rename String/{ => test}/PatternMatching.test.js (84%) rename String/{ => test}/ReverseString.test.js (98%) rename String/{ => test}/ReverseWords.test.js (90%) diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 000000000..a85cb7eb8 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,15 @@ +{ + "arrowParens": "always", + "bracketSpacing": true, + "endOfLine": "lf", + "insertPragma": false, + "printWidth": 80, + "proseWrap": "preserve", + "quoteProps": "as-needed", + "requirePragma": false, + "semi": false, + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none", + "useTabs": false +} diff --git a/Maths/Abs.js b/Maths/Abs.js index 454e4073a..4caa783ff 100644 --- a/Maths/Abs.js +++ b/Maths/Abs.js @@ -11,7 +11,7 @@ https://en.wikipedia.org/wiki/Absolute_value */ -function absVal (num) { +const absVal = (num) => { // Find absolute value of `num`. 'use strict' if (num < 0) { @@ -21,6 +21,4 @@ function absVal (num) { return num } -// Run `abs` function to find absolute value of two numbers. -console.log('The absolute value of -34 is ' + absVal(-34)) -console.log('The absolute value of 34 is ' + absVal(34)) +export { absVal } diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js index 28f96d53e..43f3f8ca0 100644 --- a/Maths/AverageMean.js +++ b/Maths/AverageMean.js @@ -14,8 +14,7 @@ const mean = (nums) => { // This is a function returns average/mean of array - var sum = 0 - var avg + let sum = 0 // This loop sums all values in the 'nums' array using forEach loop nums.forEach(function (current) { @@ -23,9 +22,8 @@ const mean = (nums) => { }) // Divide sum by the length of the 'nums' array. - avg = sum / nums.length + const avg = sum / nums.length return avg } -// Run `mean` Function to find average of a list of numbers. -console.log(mean([2, 4, 6, 8, 20, 50, 70])) +export { mean } diff --git a/Maths/digitSum.js b/Maths/DigitSum.js similarity index 70% rename from Maths/digitSum.js rename to Maths/DigitSum.js index 3f08cf807..792915d60 100644 --- a/Maths/digitSum.js +++ b/Maths/DigitSum.js @@ -1,18 +1,16 @@ // program to find sum of digits of a number // function which would calculate sum and return it -function digitSum (num) { +const digitSum = (num) => { // sum will store sum of digits of a number let sum = 0 // while will run untill num become 0 while (num) { - sum += (num % 10) + sum += num % 10 num = parseInt(num / 10) } return sum } -// assigning number -const num = 12345 -console.log(digitSum(num)) +export { digitSum } diff --git a/Maths/Factorial.js b/Maths/Factorial.js index c13878d90..04d9ef08c 100644 --- a/Maths/Factorial.js +++ b/Maths/Factorial.js @@ -13,10 +13,10 @@ 'use strict' -function calcRange (num) { +const calcRange = (num) => { // Generate a range of numbers from 1 to `num`. - var i = 1 - var range = [] + let i = 1 + const range = [] while (i <= num) { range.push(i) i += 1 @@ -24,9 +24,9 @@ function calcRange (num) { return range } -function calcFactorial (num) { - var factorial - var range = calcRange(num) +const calcFactorial = (num) => { + let factorial + const range = calcRange(num) // Check if the number is negative, positive, null, undefined, or zero if (num < 0) { @@ -43,11 +43,8 @@ function calcFactorial (num) { range.forEach(function (i) { factorial = factorial * i }) - return 'The factorial of ' + num + ' is ' + factorial + return `The factorial of ${num} is ${factorial}` } } -// Run `factorial` Function to find average of a list of numbers. - -var num = console.log('Enter a number: ') -console.log(calcFactorial(num)) +export { calcFactorial } diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 9a4361781..3a8f83381 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -66,18 +66,10 @@ const FibonacciDpWithoutRecursion = (number) => { for (var i = 2; i < number; ++i) { table.push(table[i - 1] + table[i - 2]) } - return (table) + return table } -// testing - -console.log(FibonacciIterative(5)) -// Output: [ 1, 1, 2, 3, 5 ] -console.log(FibonacciRecursive(5)) -// Output: [ 1, 1, 2, 3, 5 ] - -console.log(FibonacciRecursiveDP(5)) -// Output: 5 - -console.log(FibonacciDpWithoutRecursion(5)) -// Output: [ 1, 1, 2, 3, 5 ] +export { FibonacciDpWithoutRecursion } +export { FibonacciIterative } +export { FibonacciRecursive } +export { FibonacciRecursiveDP } diff --git a/Maths/FindHcf.js b/Maths/FindHcf.js index f6b696219..19105b5aa 100644 --- a/Maths/FindHcf.js +++ b/Maths/FindHcf.js @@ -4,7 +4,7 @@ https://en.wikipedia.org/wiki/Greatest_common_divisor */ -function findHCF (x, y) { +const findHCF = (x, y) => { // If the input numbers are less than 1 return an error message. if (x < 1 || y < 1) { return 'Please enter values greater than zero.' @@ -27,4 +27,5 @@ function findHCF (x, y) { // When the while loop finishes the minimum of x and y is the HCF. return Math.min(x, y) } -console.log(findHCF(27, 36)) + +export { findHCF } diff --git a/Maths/FindLcm.js b/Maths/FindLcm.js index 60470e37e..0d09a7aff 100644 --- a/Maths/FindLcm.js +++ b/Maths/FindLcm.js @@ -12,9 +12,19 @@ 'use strict' // Find the LCM of two numbers. -function findLcm (num1, num2) { - var maxNum - var lcm +const findLcm = (num1, num2) => { + // If the input numbers are less than 1 return an error message. + if (num1 < 1 || num2 < 1) { + return 'Please enter values greater than zero.' + } + + // If the input numbers are not integers return an error message. + if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) { + return 'Please enter whole numbers.' + } + + let maxNum + let lcm // Check to see whether num1 or num2 is larger. if (num1 > num2) { maxNum = num1 @@ -24,15 +34,10 @@ function findLcm (num1, num2) { lcm = maxNum while (true) { - if ((lcm % num1 === 0) && (lcm % num2 === 0)) { - break - } + if (lcm % num1 === 0 && lcm % num2 === 0) break lcm += maxNum } return lcm } -// Run `findLcm` Function -var num1 = 12 -var num2 = 76 -console.log(findLcm(num1, num2)) +export { findLcm } diff --git a/Maths/GridGet.js b/Maths/GridGet.js index 69ae502ef..44ae9a193 100644 --- a/Maths/GridGet.js +++ b/Maths/GridGet.js @@ -40,17 +40,14 @@ */ const gridGetX = (columns, index) => { - while ((index + 1) > columns) { + while (index + 1 > columns) { index = index - columns } - return (index + 1) + return index + 1 } const gridGetY = (columns, index) => { - return (Math.floor(index / columns)) + 1 + return Math.floor(index / columns) + 1 } -console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400), 27)}`) -console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7, 11)}`) -console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400), 27)}`) -console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7, 11)}`) +export { gridGetX, gridGetY } diff --git a/Maths/MeanSquareError.js b/Maths/MeanSquareError.js index de9dcfd7f..edcd3e699 100644 --- a/Maths/MeanSquareError.js +++ b/Maths/MeanSquareError.js @@ -18,9 +18,4 @@ const meanSquaredError = (predicted, expected) => { return err / expected.length } -// testing -(() => { - console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0) - console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5) - console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3) -})() +export { meanSquaredError } diff --git a/Maths/ModularBinaryExponentiationRecursive.js b/Maths/ModularBinaryExponentiationRecursive.js index c30ed5f2a..b8374bd17 100644 --- a/Maths/ModularBinaryExponentiationRecursive.js +++ b/Maths/ModularBinaryExponentiationRecursive.js @@ -19,13 +19,4 @@ const modularBinaryExponentiation = (a, n, m) => { } } -const main = () => { - // binary_exponentiation(2, 10, 17) - // > 4 - console.log(modularBinaryExponentiation(2, 10, 17)) - // binary_exponentiation(3, 9, 12) - // > 3 - console.log(modularBinaryExponentiation(3, 9, 12)) -} - -main() +export { modularBinaryExponentiation } diff --git a/Maths/Palindrome.js b/Maths/Palindrome.js index 4abc8d997..34804d34f 100644 --- a/Maths/Palindrome.js +++ b/Maths/Palindrome.js @@ -14,7 +14,7 @@ * @complexity: O(n) */ -function PalindromeRecursive (string) { +const PalindromeRecursive = (string) => { // Base case if (string.length < 2) return true @@ -26,7 +26,7 @@ function PalindromeRecursive (string) { return PalindromeRecursive(string.slice(1, string.length - 1)) } -function PalindromeIterative (string) { +const PalindromeIterative = (string) => { const _string = string .toLowerCase() .replace(/ /g, '') @@ -45,7 +45,4 @@ function PalindromeIterative (string) { return true } -// testing - -console.log(PalindromeRecursive('Javascript Community')) -console.log(PalindromeIterative('mom')) +export { PalindromeIterative, PalindromeRecursive } diff --git a/Maths/PascalTriangle.js b/Maths/PascalTriangle.js index eaf3f2a9e..868e36fca 100644 --- a/Maths/PascalTriangle.js +++ b/Maths/PascalTriangle.js @@ -1,6 +1,16 @@ -const numRows = 5 +const addRow = (triangle) => { + const previous = triangle[triangle.length - 1] + const newRow = [1] + for (let i = 0; i < previous.length - 1; i++) { + const current = previous[i] + const next = previous[i + 1] + newRow.push(current + next) + } + newRow.push(1) + return triangle.push(newRow) +} -var generate = function (numRows) { +const generate = (numRows) => { const triangle = [[1], [1, 1]] if (numRows === 0) { @@ -16,16 +26,5 @@ var generate = function (numRows) { } return triangle } -var addRow = function (triangle) { - const previous = triangle[triangle.length - 1] - const newRow = [1] - for (let i = 0; i < previous.length - 1; i++) { - const current = previous[i] - const next = previous[i + 1] - newRow.push(current + next) - } - newRow.push(1) - return triangle.push(newRow) -} -generate(numRows) +export { generate } diff --git a/Maths/PiApproximationMonteCarlo.js b/Maths/PiApproximationMonteCarlo.js index a4b3d8b81..be7f754e4 100644 --- a/Maths/PiApproximationMonteCarlo.js +++ b/Maths/PiApproximationMonteCarlo.js @@ -1,7 +1,7 @@ // Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method // Video Explaination: https://www.youtube.com/watch?v=ELetCV_wX_c -function piEstimation (iterations = 100000) { +const piEstimation = (iterations = 100000) => { let circleCounter = 0 for (let i = 0; i < iterations; i++) { @@ -18,8 +18,4 @@ function piEstimation (iterations = 100000) { return pi } -function main () { - console.log(piEstimation()) -} - -main() +export { piEstimation } diff --git a/Maths/Polynomial.js b/Maths/Polynomial.js index 740ac3b25..41ec340ee 100644 --- a/Maths/Polynomial.js +++ b/Maths/Polynomial.js @@ -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 } diff --git a/Maths/PrimeCheck.js b/Maths/PrimeCheck.js index d46dfab4f..c0f626ce6 100644 --- a/Maths/PrimeCheck.js +++ b/Maths/PrimeCheck.js @@ -9,6 +9,9 @@ const PrimeCheck = (n) => { // input: n: int // output: boolean + if (n === 1) return false + if (n === 0) return false + for (let i = 2; i * i <= n; i++) { if (n % i === 0) { return false @@ -17,13 +20,4 @@ const PrimeCheck = (n) => { return true } -const main = () => { - // PrimeCheck(1000003) - // > true - console.log(PrimeCheck(1000003)) - // PrimeCheck(1000001) - // > false - console.log(PrimeCheck(1000001)) -} - -main() +export { PrimeCheck } diff --git a/Maths/ReversePolishNotation.js b/Maths/ReversePolishNotation.js index ef31300a9..efe6240dc 100644 --- a/Maths/ReversePolishNotation.js +++ b/Maths/ReversePolishNotation.js @@ -1,6 +1,6 @@ // Wikipedia: https://en.wikipedia.org/wiki/Reverse_Polish_notation -function calcRPN (expression) { +const calcRPN = (expression) => { const operators = { '+': (a, b) => a + b, '-': (a, b) => a - b, @@ -12,7 +12,7 @@ function calcRPN (expression) { const stack = [] - tokens.forEach(token => { + tokens.forEach((token) => { const operator = operators[token] if (typeof operator === 'function') { @@ -30,6 +30,4 @@ function calcRPN (expression) { return stack.pop() } -console.log(calcRPN('2 2 2 * +') === 6) -console.log(calcRPN('2 2 + 2 *') === 8) -console.log(calcRPN('6 9 7 + 2 / + 3 *') === 42) +export { calcRPN } diff --git a/Maths/SieveOfEratosthenes.js b/Maths/SieveOfEratosthenes.js index 1e0a2e2b2..9393f58d8 100644 --- a/Maths/SieveOfEratosthenes.js +++ b/Maths/SieveOfEratosthenes.js @@ -1,9 +1,9 @@ -function sieveOfEratosthenes (n) { +const sieveOfEratosthenes = (n) => { /* - * Calculates prime numbers till a number n - * :param n: Number upto which to calculate primes - * :return: A boolean list contaning only primes - */ + * Calculates prime numbers till a number n + * :param n: Number upto which to calculate primes + * :return: A boolean list contaning only primes + */ const primes = new Array(n + 1) primes.fill(true) // set all as true initially primes[0] = primes[1] = false // Handling case for 0 and 1 @@ -18,14 +18,4 @@ function sieveOfEratosthenes (n) { return primes } -function main () { - const n = 69 // number till where we wish to find primes - const primes = sieveOfEratosthenes(n) - for (let i = 2; i <= n; i++) { - if (primes[i]) { - console.log(i) - } - } -} - -main() +export { sieveOfEratosthenes } diff --git a/Maths/test/Abs.test.js b/Maths/test/Abs.test.js new file mode 100644 index 000000000..116336f85 --- /dev/null +++ b/Maths/test/Abs.test.js @@ -0,0 +1,13 @@ +import { absVal } from '../Abs' + +describe('absVal', () => { + it('should return an absolute value of a negative number', () => { + const absOfNegativeNumber = absVal(-34) + expect(absOfNegativeNumber).toBe(34) + }) + + it('should return an absolute value of a positive number', () => { + const absOfPositiveNumber = absVal(50) + expect(absOfPositiveNumber).toBe(50) + }) +}) diff --git a/Maths/test/AverageMean.test.js b/Maths/test/AverageMean.test.js new file mode 100644 index 000000000..8b3d7bb13 --- /dev/null +++ b/Maths/test/AverageMean.test.js @@ -0,0 +1,11 @@ +import { mean } from '../AverageMean' + +describe('Tests for average mean', () => { + it('should be a function', () => { + expect(typeof mean).toEqual('function') + }) + it('should return the mean of an array of numbers', () => { + const meanFunction = mean([1, 2, 4, 5]) + expect(meanFunction).toBe(3) + }) +}) diff --git a/Maths/test/DigitSum.test.js b/Maths/test/DigitSum.test.js new file mode 100644 index 000000000..c9c828be5 --- /dev/null +++ b/Maths/test/DigitSum.test.js @@ -0,0 +1,11 @@ +import { digitSum } from '../DigitSum' + +describe('digitSum', () => { + it('is a function', () => { + expect(typeof digitSum).toEqual('function') + }) + it('should return the sum of digits of a given number', () => { + const sumOfNumber = digitSum(12345) + expect(sumOfNumber).toBe(15) + }) +}) diff --git a/Maths/test/Factorial.test.js b/Maths/test/Factorial.test.js new file mode 100644 index 000000000..bd22ad436 --- /dev/null +++ b/Maths/test/Factorial.test.js @@ -0,0 +1,35 @@ +import { calcFactorial } from '../Factorial' + +describe('calcFactorial', () => { + it('is a function', () => { + expect(typeof calcFactorial).toEqual('function') + }) + + it('should return a statement for value "0"', () => { + expect(calcFactorial(0)).toBe('The factorial of 0 is 1.') + }) + + it('should return a statement for "null" and "undefined"', () => { + const nullFactorial = calcFactorial(null) + const undefinedFactorial = calcFactorial(undefined) + + expect(nullFactorial).toBe( + 'Sorry, factorial does not exist for null or undefined numbers.' + ) + expect(undefinedFactorial).toBe( + 'Sorry, factorial does not exist for null or undefined numbers.' + ) + }) + + it('should not support negative numbers', () => { + const negativeFactorial = calcFactorial(-5) + expect(negativeFactorial).toBe( + 'Sorry, factorial does not exist for negative numbers.' + ) + }) + + it('should return the factorial of a positive number', () => { + const positiveFactorial = calcFactorial(3) + expect(positiveFactorial).toBe('The factorial of 3 is 6') + }) +}) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js new file mode 100644 index 000000000..e5b9376f8 --- /dev/null +++ b/Maths/test/Fibonacci.test.js @@ -0,0 +1,30 @@ +import { + FibonacciDpWithoutRecursion, + FibonacciRecursiveDP, + FibonacciIterative, + FibonacciRecursive +} from '../Fibonacci' + +describe('Fibonanci', () => { + it('should return an array of numbers for FibonnaciIterative', () => { + expect(FibonacciIterative(5)).toEqual( + expect.arrayContaining([1, 1, 2, 3, 5]) + ) + }) + + it('should return an array of numbers for FibonnaciRecursive', () => { + expect(FibonacciRecursive(5)).toEqual( + expect.arrayContaining([1, 1, 2, 3, 5]) + ) + }) + + it('should return number for FibonnaciRecursiveDP', () => { + expect(FibonacciRecursiveDP(5)).toBe(5) + }) + + it('should return an array of numbers for FibonacciDpWithoutRecursion', () => { + expect(FibonacciDpWithoutRecursion(5)).toEqual( + expect.arrayContaining([1, 1, 2, 3, 5]) + ) + }) +}) diff --git a/Maths/test/FindHcf.test.js b/Maths/test/FindHcf.test.js new file mode 100644 index 000000000..ccb5c3045 --- /dev/null +++ b/Maths/test/FindHcf.test.js @@ -0,0 +1,20 @@ +import { findHCF } from '../FindHcf' + +describe('findHCF', () => { + it('should throw a statement for values less than 1', () => { + expect(findHCF(0, 0)).toBe('Please enter values greater than zero.') + }) + + it('should throw a statement for one value less than 1', () => { + expect(findHCF(0, 1)).toBe('Please enter values greater than zero.') + expect(findHCF(1, 0)).toBe('Please enter values greater than zero.') + }) + + it('should return an error for values non-integer values', () => { + expect(findHCF(2.24, 4.35)).toBe('Please enter whole numbers.') + }) + + it('should return the HCF of two given integers', () => { + expect(findHCF(27, 36)).toBe(9) + }) +}) diff --git a/Maths/test/FindLcm.test.js b/Maths/test/FindLcm.test.js new file mode 100644 index 000000000..1e5c0905c --- /dev/null +++ b/Maths/test/FindLcm.test.js @@ -0,0 +1,20 @@ +import { findLcm } from '../FindLcm' + +describe('findLcm', () => { + it('should throw a statement for values less than 1', () => { + expect(findLcm(0, 0)).toBe('Please enter values greater than zero.') + }) + + it('should throw a statement for one value less than 1', () => { + expect(findLcm(1, 0)).toBe('Please enter values greater than zero.') + expect(findLcm(0, 1)).toBe('Please enter values greater than zero.') + }) + + it('should return an error for values non-integer values', () => { + expect(findLcm(4.564, 7.39)).toBe('Please enter whole numbers.') + }) + + it('should return the LCM of two given integers', () => { + expect(findLcm(27, 36)).toBe(108) + }) +}) diff --git a/Maths/test/GridGet.test.js b/Maths/test/GridGet.test.js new file mode 100644 index 000000000..eef51fc6f --- /dev/null +++ b/Maths/test/GridGet.test.js @@ -0,0 +1,16 @@ +import { gridGetX, gridGetY } from '../GridGet' + +describe('GridGet', () => { + it('should have a value of x for the 27th element if the square array has 400 elements', () => { + expect(gridGetX(Math.sqrt(400), 27)).toEqual(8) + }) + it('should have a value of x for the 11th element if the square array has 7 columns and 3 rows', () => { + expect(gridGetX(7, 11)).toEqual(5) + }) + it('should have a value of y for the 27th element if the square array has 400 elements', () => { + expect(gridGetY(Math.sqrt(400), 27)).toEqual(2) + }) + it('should have a value of y for the 11th element if the square array has 7 columns and 3 rows ', () => { + expect(gridGetX(7, 11)).toEqual(5) + }) +}) diff --git a/Maths/test/MeanSquareError.test.js b/Maths/test/MeanSquareError.test.js new file mode 100644 index 000000000..ecd53de89 --- /dev/null +++ b/Maths/test/MeanSquareError.test.js @@ -0,0 +1,21 @@ +import { meanSquaredError } from '../MeanSquareError' + +describe('meanSquareError', () => { + it('should throw an error on non-array arguments', () => { + expect(() => meanSquaredError(1, 4)).toThrow('Argument must be an Array') + }) + + it('should throw an error on non equal length ', () => { + const firstArr = [1, 2, 3, 4, 5] + const secondArr = [1, 2, 3] + expect(() => meanSquaredError(firstArr, secondArr)).toThrow( + 'The two lists must be of equal length' + ) + }) + + it('should return the mean square error of two equal length arrays', () => { + const firstArr = [1, 2, 3, 4, 5] + const secondArr = [1, 3, 5, 6, 7] + expect(meanSquaredError(firstArr, secondArr)).toBe(2.6) + }) +}) diff --git a/Maths/test/ModularBinaryExponentiationRecursive.test.js b/Maths/test/ModularBinaryExponentiationRecursive.test.js new file mode 100644 index 000000000..9758d0ed1 --- /dev/null +++ b/Maths/test/ModularBinaryExponentiationRecursive.test.js @@ -0,0 +1,7 @@ +import { modularBinaryExponentiation } from '../ModularBinaryExponentiationRecursive' + +describe('modularBinaryExponentiation', () => { + it('should return the binary exponentiation', () => { + expect(modularBinaryExponentiation(2, 10, 17)).toBe(4) + }) +}) diff --git a/Maths/test/Palindrome.test.js b/Maths/test/Palindrome.test.js new file mode 100644 index 000000000..53cb88395 --- /dev/null +++ b/Maths/test/Palindrome.test.js @@ -0,0 +1,16 @@ +import { PalindromeRecursive, PalindromeIterative } from '../Palindrome' + +describe('Palindrome', () => { + it('should return true for a palindrome for PalindromeRecursive', () => { + expect(PalindromeRecursive('mom')).toBeTruthy() + }) + it('should return true for a palindrome for PalindromeIterative', () => { + expect(PalindromeIterative('mom')).toBeTruthy() + }) + it('should return false for a non-palindrome for PalindromeRecursive', () => { + expect(PalindromeRecursive('Algorithms')).toBeFalsy() + }) + it('should return true for a non-palindrome for PalindromeIterative', () => { + expect(PalindromeIterative('JavaScript')).toBeFalsy() + }) +}) diff --git a/Maths/test/PascalTriangle.test.js b/Maths/test/PascalTriangle.test.js new file mode 100644 index 000000000..314d0f321 --- /dev/null +++ b/Maths/test/PascalTriangle.test.js @@ -0,0 +1,20 @@ +import { generate } from '../PascalTriangle' + +describe('Pascals Triangle', () => { + it('should have the the same length as the number', () => { + const pascalsTriangle = generate(5) + expect(pascalsTriangle.length).toEqual(5) + }) + it('should have same length as its index in the array', () => { + const pascalsTriangle = generate(5) + pascalsTriangle.forEach((arr, index) => { + expect(arr.length).toEqual(index + 1) + }) + }) + it('should return an array of arrays', () => { + const pascalsTriangle = generate(3) + expect(pascalsTriangle).toEqual( + expect.arrayContaining([[1], [1, 1], [1, 2, 1]]) + ) + }) +}) diff --git a/Maths/test/PiApproximationMonteCarlo.test.js b/Maths/test/PiApproximationMonteCarlo.test.js new file mode 100644 index 000000000..9727aa578 --- /dev/null +++ b/Maths/test/PiApproximationMonteCarlo.test.js @@ -0,0 +1,9 @@ +import { piEstimation } from '../PiApproximationMonteCarlo' + +describe('PiApproximationMonteCarlo', () => { + it('should be between the range of 2 to 4', () => { + const pi = piEstimation() + const piRange = pi >= 2 && pi <= 4 + expect(piRange).toBeTruthy() + }) +}) diff --git a/Maths/test/Polynomial.test.js b/Maths/test/Polynomial.test.js new file mode 100644 index 000000000..af5618ab3 --- /dev/null +++ b/Maths/test/Polynomial.test.js @@ -0,0 +1,37 @@ +import { Polynomial } from '../Polynomial' + +describe('Polynomial', () => { + it('should not return a expression for zero', () => { + const polynomial = new Polynomial([0]) + expect(polynomial.display()).toBe('') + }) + it('should not return an expression for zero values', () => { + const polynomial = new Polynomial([0, 0, 0, 0, 0]) + expect(polynomial.display()).toBe('') + }) + it('should return an expression for single a non zero value', () => { + const polynomial = new Polynomial([9]) + expect(polynomial.display()).toBe('(9)') + }) + it('should return an expression for two values', () => { + const polynomial = new Polynomial([3, 2]) + expect(polynomial.display()).toBe('(2x) + (3)') + }) + it('should return an expression for values including zero', () => { + const polynomial = new Polynomial([0, 2]) + expect(polynomial.display()).toBe('(2x)') + }) + it('should return an expression and evaluate it', () => { + const polynomial = new Polynomial([1, 2, 3, 4]) + expect(polynomial.display()).toBe('(4x^3) + (3x^2) + (2x) + (1)') + expect(polynomial.evaluate(2)).toEqual(49) + }) + it('should evaluate 0 for zero values', () => { + const polynomial = new Polynomial([0, 0, 0, 0]) + expect(polynomial.evaluate(5)).toEqual(0) + }) + it('should evaluate for negative values', () => { + const polynomial = new Polynomial([-1, -3, -4, -7]) + expect(polynomial.evaluate(-5)).toBe(789) + }) +}) diff --git a/Maths/test/PrimeCheck.test.js b/Maths/test/PrimeCheck.test.js new file mode 100644 index 000000000..da1cd1b52 --- /dev/null +++ b/Maths/test/PrimeCheck.test.js @@ -0,0 +1,14 @@ +import { PrimeCheck } from '../PrimeCheck' + +describe('PrimeCheck', () => { + it('should return true for Prime Numbers', () => { + expect(PrimeCheck(1000003)).toBeTruthy() + }) + it('should return false for Non Prime Numbers', () => { + expect(PrimeCheck(1000001)).toBeFalsy() + }) + it('should return false for 1 and 0', () => { + expect(PrimeCheck(1)).toBeFalsy() + expect(PrimeCheck(0)).toBeFalsy() + }) +}) diff --git a/Maths/test/ReversePolishNotation.test.js b/Maths/test/ReversePolishNotation.test.js new file mode 100644 index 000000000..8b880ee47 --- /dev/null +++ b/Maths/test/ReversePolishNotation.test.js @@ -0,0 +1,11 @@ +import { calcRPN } from '../ReversePolishNotation' + +describe('ReversePolishNotation', () => { + it('should evaluate correctly for two values', () => { + expect(calcRPN('2 3 +')).toEqual(5) + }) + it("should evaluate' for multiple values", () => { + expect(calcRPN('2 2 2 * +')).toEqual(6) + expect(calcRPN('6 9 7 + 2 / + 3 *')).toEqual(42) + }) +}) diff --git a/Maths/test/SieveOfEratosthenes.test.js b/Maths/test/SieveOfEratosthenes.test.js new file mode 100644 index 000000000..056693d39 --- /dev/null +++ b/Maths/test/SieveOfEratosthenes.test.js @@ -0,0 +1,14 @@ +import { sieveOfEratosthenes } from '../SieveOfEratosthenes' +import { PrimeCheck } from '../PrimeCheck' + +describe('should return an array of prime booleans', () => { + it('should have each element in the array as a prime boolean', () => { + const n = 30 + const primes = sieveOfEratosthenes(n) + primes.forEach((primeBool, index) => { + if (primeBool) { + expect(PrimeCheck(index)).toBeTruthy() + } + }) + }) +}) diff --git a/String/PatternMatching.js b/String/PatternMatching.js index 3148f85cd..b90260ce7 100644 --- a/String/PatternMatching.js +++ b/String/PatternMatching.js @@ -24,7 +24,6 @@ const checkIfPatternExists = (text, pattern) => { // For each iteration of j check if the value of // j + 1 is equal to the length of the pattern if (j + 1 === patternLength) { - console.log(`Given pattern is found at index ${i}`) return `Given pattern is found at index ${i}` } } diff --git a/String/CheckAnagram.test.js b/String/test/CheckAnagram.test.js similarity index 92% rename from String/CheckAnagram.test.js rename to String/test/CheckAnagram.test.js index e5016f752..691d5ba89 100644 --- a/String/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -1,4 +1,4 @@ -import { checkAnagram } from './CheckAnagram' +import { checkAnagram } from '../CheckAnagram' describe('checkAnagram', () => { it.each` @@ -18,7 +18,7 @@ describe('checkAnagram', () => { ) it('expects to return "Not anagram" if the arguments have different lengths', () => { const SUT = checkAnagram('abs', 'abds') - expect(SUT).toBe('Not Anagram') + expect(SUT).toBe('Not anagrams') }) it('expects to return "Not anagram" if the arguments are not anagrams', () => { const SUT = checkAnagram('abcs', 'abds') diff --git a/String/CheckPalindrome.test.js b/String/test/CheckPalindrome.test.js similarity index 90% rename from String/CheckPalindrome.test.js rename to String/test/CheckPalindrome.test.js index 3bd401ba1..cfe88f7e5 100644 --- a/String/CheckPalindrome.test.js +++ b/String/test/CheckPalindrome.test.js @@ -1,4 +1,4 @@ -import { checkPalindrome } from './CheckPalindrome' +import { checkPalindrome } from '../CheckPalindrome' describe('checkPalindrome', () => { it('expects to return "Palindrome" if the given string is a palindrome', () => { diff --git a/String/PatternMatching.test.js b/String/test/PatternMatching.test.js similarity index 84% rename from String/PatternMatching.test.js rename to String/test/PatternMatching.test.js index 23e892dd7..d0eab80b6 100644 --- a/String/PatternMatching.test.js +++ b/String/test/PatternMatching.test.js @@ -1,4 +1,4 @@ -import { checkIfPatternExists } from './PatternMatching' +import { checkIfPatternExists } from '../PatternMatching' describe('checkIfPatternExists', () => { it('expects to find a pattern with correct input', () => { const text = 'AABAACAADAABAAAABAA' @@ -21,6 +21,8 @@ describe('checkIfPatternExists', () => { it('expects to throw an error message when given inpuut is not a string', () => { const text = 123444456 const pattern = 123 - expect(() => checkIfPatternExists(text, pattern)).toThrow('Given input is not a string') + expect(() => checkIfPatternExists(text, pattern)).toThrow( + 'Given input is not a string' + ) }) }) diff --git a/String/ReverseString.test.js b/String/test/ReverseString.test.js similarity index 98% rename from String/ReverseString.test.js rename to String/test/ReverseString.test.js index 1658c8bde..6ec1bb4df 100644 --- a/String/ReverseString.test.js +++ b/String/test/ReverseString.test.js @@ -1,7 +1,7 @@ import { ReverseStringIterative, ReverseStringIterativeInplace -} from './ReverseString' +} from '../ReverseString' describe('ReverseStringIterative', () => { it('expects to reverse a simple string', () => { diff --git a/String/ReverseWords.test.js b/String/test/ReverseWords.test.js similarity index 90% rename from String/ReverseWords.test.js rename to String/test/ReverseWords.test.js index 7c4aa16dd..32bd9d462 100644 --- a/String/ReverseWords.test.js +++ b/String/test/ReverseWords.test.js @@ -1,4 +1,4 @@ -import { reverseWords } from './ReverseWords' +import { reverseWords } from '../ReverseWords' describe('reverseWords', () => { it('expects to reverse words to return a joined word', () => {