From f5e68a836fc59121cfc39770fee25da290e379d7 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 19:05:52 +0000 Subject: [PATCH 01/21] Add prettier config --- .prettierrc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000000..d6688ba6f9 --- /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": "es5", + "useTabs": false +} From 3a94b317123b9027d4421b2deaa1863348ea1d43 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 19:14:46 +0000 Subject: [PATCH 02/21] test: add test to check for absolute function --- Maths/Abs.js | 6 ++---- Maths/Abs.test.js | 13 +++++++++++++ package.json | 4 +++- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Maths/Abs.test.js diff --git a/Maths/Abs.js b/Maths/Abs.js index 454e4073a6..4caa783ff0 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/Abs.test.js b/Maths/Abs.test.js new file mode 100644 index 0000000000..a66e53490a --- /dev/null +++ b/Maths/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/package.json b/package.json index eea349d1b3..450a58349e 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,9 @@ "node-fetch": "2.6.1" }, "standard": { - "env": [ "jest" ] + "env": [ + "jest" + ] }, "devDependencies": { "babel-jest": "^26.3.0", From e52a2d36d85cd5c42c15d4ba7ad670eba18a90a7 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 20:56:50 +0000 Subject: [PATCH 03/21] chore: es5 to es6 --- Maths/AverageMean.js | 4 ++-- Maths/Factorial.js | 14 +++++++------- Maths/FindHcf.js | 2 +- Maths/FindLcm.js | 6 +++--- Maths/Palindrome.js | 4 ++-- Maths/PascalTriangle.js | 25 +++++++++++++------------ Maths/PiApproximationMonteCarlo.js | 4 ++-- Maths/SieveOfEratosthenes.js | 4 ++-- Maths/digitSum.js | 2 +- 9 files changed, 33 insertions(+), 32 deletions(-) diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js index 28f96d53ef..d5bf322967 100644 --- a/Maths/AverageMean.js +++ b/Maths/AverageMean.js @@ -14,8 +14,8 @@ const mean = (nums) => { // This is a function returns average/mean of array - var sum = 0 - var avg + let sum = 0 + let avg // This loop sums all values in the 'nums' array using forEach loop nums.forEach(function (current) { diff --git a/Maths/Factorial.js b/Maths/Factorial.js index c13878d905..fcfc086e3d 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 + let 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 + let range = calcRange(num) // Check if the number is negative, positive, null, undefined, or zero if (num < 0) { @@ -43,7 +43,7 @@ function calcFactorial (num) { range.forEach(function (i) { factorial = factorial * i }) - return 'The factorial of ' + num + ' is ' + factorial + return `The factorial of ${num} is ${factorial}` } } diff --git a/Maths/FindHcf.js b/Maths/FindHcf.js index f6b696219e..400d021bda 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.' diff --git a/Maths/FindLcm.js b/Maths/FindLcm.js index 60470e37ee..b364724929 100644 --- a/Maths/FindLcm.js +++ b/Maths/FindLcm.js @@ -12,9 +12,9 @@ 'use strict' // Find the LCM of two numbers. -function findLcm (num1, num2) { - var maxNum - var lcm +const findLcm = (num1, num2) => { + let maxNum + let lcm // Check to see whether num1 or num2 is larger. if (num1 > num2) { maxNum = num1 diff --git a/Maths/Palindrome.js b/Maths/Palindrome.js index 4abc8d9976..dab85da3c0 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, '') diff --git a/Maths/PascalTriangle.js b/Maths/PascalTriangle.js index eaf3f2a9e0..077296292d 100644 --- a/Maths/PascalTriangle.js +++ b/Maths/PascalTriangle.js @@ -1,6 +1,18 @@ const numRows = 5 -var generate = function (numRows) { +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) +} + +const generate = (numRows) => { const triangle = [[1], [1, 1]] if (numRows === 0) { @@ -16,16 +28,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) diff --git a/Maths/PiApproximationMonteCarlo.js b/Maths/PiApproximationMonteCarlo.js index a4b3d8b81f..06242bad24 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,7 +18,7 @@ function piEstimation (iterations = 100000) { return pi } -function main () { +function main() { console.log(piEstimation()) } diff --git a/Maths/SieveOfEratosthenes.js b/Maths/SieveOfEratosthenes.js index 1e0a2e2b21..da20f86a01 100644 --- a/Maths/SieveOfEratosthenes.js +++ b/Maths/SieveOfEratosthenes.js @@ -1,4 +1,4 @@ -function sieveOfEratosthenes (n) { +const sieveOfEratosthenes = (n) => { /* * Calculates prime numbers till a number n * :param n: Number upto which to calculate primes @@ -18,7 +18,7 @@ function sieveOfEratosthenes (n) { return primes } -function main () { +const main = () => { const n = 69 // number till where we wish to find primes const primes = sieveOfEratosthenes(n) for (let i = 2; i <= n; i++) { diff --git a/Maths/digitSum.js b/Maths/digitSum.js index 3f08cf8070..edfb51017c 100644 --- a/Maths/digitSum.js +++ b/Maths/digitSum.js @@ -1,7 +1,7 @@ // 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 From 46bc72e7e3e54ac308d0f101e91873a041e9108d Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 21:14:12 +0000 Subject: [PATCH 04/21] test: add test to check mean function --- Maths/AverageMean.js | 3 +-- Maths/AverageMean.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Maths/AverageMean.test.js diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js index d5bf322967..05963f995c 100644 --- a/Maths/AverageMean.js +++ b/Maths/AverageMean.js @@ -27,5 +27,4 @@ const mean = (nums) => { 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/AverageMean.test.js b/Maths/AverageMean.test.js new file mode 100644 index 0000000000..7d80bc3a59 --- /dev/null +++ b/Maths/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) + }) +}) From f6ec08d403d02f655dcda4ead7e7473599c98142 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 21:29:01 +0000 Subject: [PATCH 05/21] test: add test for sum of digit --- Maths/{digitSum.js => DigitSum.js} | 6 ++---- Maths/DigitSum.test.js | 11 +++++++++++ String/CheckAnagram.test.js | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) rename Maths/{digitSum.js => DigitSum.js} (77%) create mode 100644 Maths/DigitSum.test.js diff --git a/Maths/digitSum.js b/Maths/DigitSum.js similarity index 77% rename from Maths/digitSum.js rename to Maths/DigitSum.js index edfb51017c..792915d606 100644 --- a/Maths/digitSum.js +++ b/Maths/DigitSum.js @@ -6,13 +6,11 @@ const digitSum = (num) => { 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/DigitSum.test.js b/Maths/DigitSum.test.js new file mode 100644 index 0000000000..d3c7052e42 --- /dev/null +++ b/Maths/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/String/CheckAnagram.test.js b/String/CheckAnagram.test.js index e5016f752a..65d8ed7d1e 100644 --- a/String/CheckAnagram.test.js +++ b/String/CheckAnagram.test.js @@ -18,11 +18,11 @@ 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 anagram') }) it('expects to return "Not anagram" if the arguments are not anagrams', () => { const SUT = checkAnagram('abcs', 'abds') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe('Not anagram') }) it('expects to return "Anagram" if the arguments are anagram', () => { const SUT = checkAnagram('abcd', 'bcad') From 277ba490d0ff667bd241287f8e9cdcc153e372b2 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 21:44:00 +0000 Subject: [PATCH 06/21] test: add test for factorial --- Maths/Factorial.js | 5 +---- Maths/Factorial.test.js | 35 +++++++++++++++++++++++++++++++++++ String/CheckAnagram.test.js | 4 ++-- 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 Maths/Factorial.test.js diff --git a/Maths/Factorial.js b/Maths/Factorial.js index fcfc086e3d..f1c433886e 100644 --- a/Maths/Factorial.js +++ b/Maths/Factorial.js @@ -47,7 +47,4 @@ const calcFactorial = (num) => { } } -// 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/Factorial.test.js b/Maths/Factorial.test.js new file mode 100644 index 0000000000..ec085d1d74 --- /dev/null +++ b/Maths/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/String/CheckAnagram.test.js b/String/CheckAnagram.test.js index 65d8ed7d1e..ebb1ee5f9f 100644 --- a/String/CheckAnagram.test.js +++ b/String/CheckAnagram.test.js @@ -18,11 +18,11 @@ 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') - expect(SUT).toBe('Not anagram') + expect(SUT).toBe('Not anagrams') }) it('expects to return "Anagram" if the arguments are anagram', () => { const SUT = checkAnagram('abcd', 'bcad') From ce05bd51172da810063bbf638acdce682e773c26 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 22:18:08 +0000 Subject: [PATCH 07/21] test: add test for fibonnaci --- Maths/Fibonacci.js | 18 +++++------------- Maths/Fibonacci.test.js | 30 ++++++++++++++++++++++++++++++ String/PatternMatching.js | 1 - 3 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 Maths/Fibonacci.test.js diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 9a4361781d..3a8f833811 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/Fibonacci.test.js b/Maths/Fibonacci.test.js new file mode 100644 index 0000000000..cad3443736 --- /dev/null +++ b/Maths/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/String/PatternMatching.js b/String/PatternMatching.js index 3148f85cd3..b90260ce7c 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}` } } From 292d391dbd6cec377aa7615f76b1bb27b97e6deb Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 22:36:14 +0000 Subject: [PATCH 08/21] test: add test for find HCF --- Maths/FindHcf.js | 3 ++- Maths/FindHcf.test.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Maths/FindHcf.test.js diff --git a/Maths/FindHcf.js b/Maths/FindHcf.js index 400d021bda..19105b5aa6 100644 --- a/Maths/FindHcf.js +++ b/Maths/FindHcf.js @@ -27,4 +27,5 @@ const 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/FindHcf.test.js b/Maths/FindHcf.test.js new file mode 100644 index 0000000000..d2bc9dc619 --- /dev/null +++ b/Maths/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) + }) +}) From ad49e33f38f0d903bddeff80729df3a1afd90a31 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 22:45:55 +0000 Subject: [PATCH 09/21] test: add test for lcm --- Maths/FindLcm.js | 19 ++++++++++++------- Maths/FindLcm.test.js | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 Maths/FindLcm.test.js diff --git a/Maths/FindLcm.js b/Maths/FindLcm.js index b364724929..0d09a7affb 100644 --- a/Maths/FindLcm.js +++ b/Maths/FindLcm.js @@ -13,6 +13,16 @@ // Find the LCM of two numbers. 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. @@ -24,15 +34,10 @@ const 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/FindLcm.test.js b/Maths/FindLcm.test.js new file mode 100644 index 0000000000..cfd0877ed8 --- /dev/null +++ b/Maths/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) + }) +}) From 7ff713c95f5c0f6c8a0c14be1be81fa8a651f9fc Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 22:57:58 +0000 Subject: [PATCH 10/21] test: add gridget test --- Maths/GridGet.js | 11 ++++------- Maths/GridGet.test.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 Maths/GridGet.test.js diff --git a/Maths/GridGet.js b/Maths/GridGet.js index 69ae502efd..44ae9a1931 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/GridGet.test.js b/Maths/GridGet.test.js new file mode 100644 index 0000000000..2c3cfb1ead --- /dev/null +++ b/Maths/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) + }) +}) From a4fa85995604e6d641b6c7aad060ec2c4af9e721 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 23:11:01 +0000 Subject: [PATCH 11/21] test: add test for mean square error --- Maths/MeanSquareError.js | 7 +------ Maths/MeanSquareError.test.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 Maths/MeanSquareError.test.js diff --git a/Maths/MeanSquareError.js b/Maths/MeanSquareError.js index de9dcfd7f0..edcd3e6993 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/MeanSquareError.test.js b/Maths/MeanSquareError.test.js new file mode 100644 index 0000000000..7a19a5571f --- /dev/null +++ b/Maths/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) + }) +}) From 817427ed6ff74c55935ee8d3629b5c84870e20bd Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 23:16:40 +0000 Subject: [PATCH 12/21] test: add test for modular binary exponentiation --- Maths/ModularBinaryExponentiationRecursive.js | 11 +---------- Maths/ModularBinaryExponentiationRecursive.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 10 deletions(-) create mode 100644 Maths/ModularBinaryExponentiationRecursive.test.js diff --git a/Maths/ModularBinaryExponentiationRecursive.js b/Maths/ModularBinaryExponentiationRecursive.js index c30ed5f2ab..b8374bd175 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/ModularBinaryExponentiationRecursive.test.js b/Maths/ModularBinaryExponentiationRecursive.test.js new file mode 100644 index 0000000000..2b541a887f --- /dev/null +++ b/Maths/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) + }) +}) From 989dd7eab23e6816b90c703887fe0db9944b937b Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Mon, 5 Oct 2020 23:37:39 +0000 Subject: [PATCH 13/21] test: add tests for palindrome --- Maths/Palindrome.js | 5 +---- Maths/Palindrome.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 Maths/Palindrome.test.js diff --git a/Maths/Palindrome.js b/Maths/Palindrome.js index dab85da3c0..34804d34fb 100644 --- a/Maths/Palindrome.js +++ b/Maths/Palindrome.js @@ -45,7 +45,4 @@ const PalindromeIterative = (string) => { return true } -// testing - -console.log(PalindromeRecursive('Javascript Community')) -console.log(PalindromeIterative('mom')) +export { PalindromeIterative, PalindromeRecursive } diff --git a/Maths/Palindrome.test.js b/Maths/Palindrome.test.js new file mode 100644 index 0000000000..44717d4c60 --- /dev/null +++ b/Maths/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() + }) +}) From d022e3632e43a6ea214a690e4fd21f179b3d09af Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 00:01:01 +0000 Subject: [PATCH 14/21] test: add test for pascals triangle --- Maths/PascalTriangle.js | 4 +--- Maths/PascalTriangle.test.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 Maths/PascalTriangle.test.js diff --git a/Maths/PascalTriangle.js b/Maths/PascalTriangle.js index 077296292d..868e36fcac 100644 --- a/Maths/PascalTriangle.js +++ b/Maths/PascalTriangle.js @@ -1,5 +1,3 @@ -const numRows = 5 - const addRow = (triangle) => { const previous = triangle[triangle.length - 1] const newRow = [1] @@ -29,4 +27,4 @@ const generate = (numRows) => { return triangle } -generate(numRows) +export { generate } diff --git a/Maths/PascalTriangle.test.js b/Maths/PascalTriangle.test.js new file mode 100644 index 0000000000..15c4455344 --- /dev/null +++ b/Maths/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]]) + ) + }) +}) From 35603342240e8a41cfa0500d4847e332567228b3 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 10:02:08 +0000 Subject: [PATCH 15/21] test: add tests for polynomial --- Maths/PiApproximationMonteCarlo.js | 6 +--- Maths/Polynomial.js | 57 ++++++++++-------------------- Maths/Polynomial.test.js | 37 +++++++++++++++++++ 3 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 Maths/Polynomial.test.js diff --git a/Maths/PiApproximationMonteCarlo.js b/Maths/PiApproximationMonteCarlo.js index 06242bad24..be7f754e4a 100644 --- a/Maths/PiApproximationMonteCarlo.js +++ b/Maths/PiApproximationMonteCarlo.js @@ -18,8 +18,4 @@ const piEstimation = (iterations = 100000) => { return pi } -function main() { - console.log(piEstimation()) -} - -main() +export { piEstimation } diff --git a/Maths/Polynomial.js b/Maths/Polynomial.js index 740ac3b25e..89c287bfc3 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: @@ -10,7 +9,7 @@ * The members of array are coefficients and their indexes as exponents. */ class Polynomial { - constructor (array) { + constructor(array) { this.coefficientArray = array // array of coefficients this.polynomial = '' // in terms of x e.g. (2x) + (1) this.construct() @@ -19,19 +18,20 @@ 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})` - } - }) + 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})` + } + }) .filter((x) => { if (x !== '0') { return x @@ -45,7 +45,7 @@ class Polynomial { * Function to display polynomial in terms of x * @returns {String} of polynomial representation in terms of x */ - display () { + display() { return this.polynomial } @@ -53,30 +53,11 @@ class Polynomial { * Function to calculate the value of the polynomial by substituting variable x * @param {Number} value */ - evaluate (value) { + 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/Polynomial.test.js b/Maths/Polynomial.test.js new file mode 100644 index 0000000000..ed7b305233 --- /dev/null +++ b/Maths/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) + }) +}) From 9386e358f94c8f096c3ce513193e94ad44ccba7c Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 10:19:02 +0000 Subject: [PATCH 16/21] test: add tests for prime check --- Maths/PrimeCheck.js | 14 ++++---------- Maths/PrimeCheck.test.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 Maths/PrimeCheck.test.js diff --git a/Maths/PrimeCheck.js b/Maths/PrimeCheck.js index d46dfab4fa..c0f626ce60 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/PrimeCheck.test.js b/Maths/PrimeCheck.test.js new file mode 100644 index 0000000000..e7aafcaf57 --- /dev/null +++ b/Maths/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() + }) +}) From c152297b65921bd344f70c3f01845f8889a93b9c Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 10:50:04 +0000 Subject: [PATCH 17/21] test: add tests for reverse polish notation --- Maths/ReversePolishNotation.js | 10 ++++------ Maths/ReversePolishNotation.test.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 Maths/ReversePolishNotation.test.js diff --git a/Maths/ReversePolishNotation.js b/Maths/ReversePolishNotation.js index ef31300a9d..565db012b5 100644 --- a/Maths/ReversePolishNotation.js +++ b/Maths/ReversePolishNotation.js @@ -1,18 +1,18 @@ // 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, '*': (a, b) => a * b, - '/': (a, b) => b / a + '/': (a, b) => b / a, } const tokens = expression.split(' ') 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/ReversePolishNotation.test.js b/Maths/ReversePolishNotation.test.js new file mode 100644 index 0000000000..91882b3b01 --- /dev/null +++ b/Maths/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) + }) +}) From 70923a4f3d5a5619f59e72dee577ebb3bc4578d4 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 10:56:15 +0000 Subject: [PATCH 18/21] test: add tests for sieve of eratosthenes --- Maths/SieveOfEratosthenes.js | 20 +++++--------------- Maths/SieveOfEratosthenes.test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 Maths/SieveOfEratosthenes.test.js diff --git a/Maths/SieveOfEratosthenes.js b/Maths/SieveOfEratosthenes.js index da20f86a01..9393f58d8c 100644 --- a/Maths/SieveOfEratosthenes.js +++ b/Maths/SieveOfEratosthenes.js @@ -1,9 +1,9 @@ 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 @@ const sieveOfEratosthenes = (n) => { return primes } -const 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/SieveOfEratosthenes.test.js b/Maths/SieveOfEratosthenes.test.js new file mode 100644 index 0000000000..0f4de8ac45 --- /dev/null +++ b/Maths/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 + } + }) + }) +}) From 0e6d38715d86cc1a0d199697d0cf16e71eb110e9 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 11:05:45 +0000 Subject: [PATCH 19/21] test: add tests for pi estimation monte carlo method --- Maths/PiApproximationMonteCarlo.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Maths/PiApproximationMonteCarlo.test.js diff --git a/Maths/PiApproximationMonteCarlo.test.js b/Maths/PiApproximationMonteCarlo.test.js new file mode 100644 index 0000000000..5eef3d789d --- /dev/null +++ b/Maths/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() + }) +}) From 4120722c7ab8c5a18dd37a094a92344c4038da2f Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 11:10:22 +0000 Subject: [PATCH 20/21] chore: move tests to test folder --- Maths/{ => test}/Abs.test.js | 2 +- Maths/{ => test}/AverageMean.test.js | 2 +- Maths/{ => test}/DigitSum.test.js | 2 +- Maths/{ => test}/Factorial.test.js | 2 +- Maths/{ => test}/Fibonacci.test.js | 2 +- Maths/{ => test}/FindHcf.test.js | 2 +- Maths/{ => test}/FindLcm.test.js | 2 +- Maths/{ => test}/GridGet.test.js | 2 +- Maths/{ => test}/MeanSquareError.test.js | 2 +- .../{ => test}/ModularBinaryExponentiationRecursive.test.js | 2 +- Maths/{ => test}/Palindrome.test.js | 2 +- Maths/{ => test}/PascalTriangle.test.js | 2 +- Maths/{ => test}/PiApproximationMonteCarlo.test.js | 2 +- Maths/{ => test}/Polynomial.test.js | 2 +- Maths/{ => test}/PrimeCheck.test.js | 2 +- Maths/{ => test}/ReversePolishNotation.test.js | 2 +- Maths/{ => test}/SieveOfEratosthenes.test.js | 4 ++-- String/{ => test}/CheckAnagram.test.js | 2 +- String/{ => test}/CheckPalindrome.test.js | 2 +- String/{ => test}/PatternMatching.test.js | 6 ++++-- String/{ => test}/ReverseString.test.js | 4 ++-- String/{ => test}/ReverseWords.test.js | 2 +- 22 files changed, 27 insertions(+), 25 deletions(-) rename Maths/{ => test}/Abs.test.js (91%) rename Maths/{ => test}/AverageMean.test.js (87%) rename Maths/{ => test}/DigitSum.test.js (86%) rename Maths/{ => test}/Factorial.test.js (95%) rename Maths/{ => test}/Fibonacci.test.js (97%) rename Maths/{ => test}/FindHcf.test.js (94%) rename Maths/{ => test}/FindLcm.test.js (94%) rename Maths/{ => test}/GridGet.test.js (93%) rename Maths/{ => test}/MeanSquareError.test.js (92%) rename Maths/{ => test}/ModularBinaryExponentiationRecursive.test.js (66%) rename Maths/{ => test}/Palindrome.test.js (88%) rename Maths/{ => test}/PascalTriangle.test.js (92%) rename Maths/{ => test}/PiApproximationMonteCarlo.test.js (77%) rename Maths/{ => test}/Polynomial.test.js (96%) rename Maths/{ => test}/PrimeCheck.test.js (89%) rename Maths/{ => test}/ReversePolishNotation.test.js (85%) rename Maths/{ => test}/SieveOfEratosthenes.test.js (75%) rename String/{ => test}/CheckAnagram.test.js (95%) rename String/{ => test}/CheckPalindrome.test.js (90%) rename String/{ => test}/PatternMatching.test.js (84%) rename String/{ => test}/ReverseString.test.js (96%) rename String/{ => test}/ReverseWords.test.js (90%) diff --git a/Maths/Abs.test.js b/Maths/test/Abs.test.js similarity index 91% rename from Maths/Abs.test.js rename to Maths/test/Abs.test.js index a66e53490a..116336f855 100644 --- a/Maths/Abs.test.js +++ b/Maths/test/Abs.test.js @@ -1,4 +1,4 @@ -import { absVal } from './Abs' +import { absVal } from '../Abs' describe('absVal', () => { it('should return an absolute value of a negative number', () => { diff --git a/Maths/AverageMean.test.js b/Maths/test/AverageMean.test.js similarity index 87% rename from Maths/AverageMean.test.js rename to Maths/test/AverageMean.test.js index 7d80bc3a59..8b3d7bb132 100644 --- a/Maths/AverageMean.test.js +++ b/Maths/test/AverageMean.test.js @@ -1,4 +1,4 @@ -import { mean } from './AverageMean' +import { mean } from '../AverageMean' describe('Tests for average mean', () => { it('should be a function', () => { diff --git a/Maths/DigitSum.test.js b/Maths/test/DigitSum.test.js similarity index 86% rename from Maths/DigitSum.test.js rename to Maths/test/DigitSum.test.js index d3c7052e42..c9c828be5f 100644 --- a/Maths/DigitSum.test.js +++ b/Maths/test/DigitSum.test.js @@ -1,4 +1,4 @@ -import { digitSum } from './DigitSum' +import { digitSum } from '../DigitSum' describe('digitSum', () => { it('is a function', () => { diff --git a/Maths/Factorial.test.js b/Maths/test/Factorial.test.js similarity index 95% rename from Maths/Factorial.test.js rename to Maths/test/Factorial.test.js index ec085d1d74..bd22ad4361 100644 --- a/Maths/Factorial.test.js +++ b/Maths/test/Factorial.test.js @@ -1,4 +1,4 @@ -import { calcFactorial } from './Factorial' +import { calcFactorial } from '../Factorial' describe('calcFactorial', () => { it('is a function', () => { diff --git a/Maths/Fibonacci.test.js b/Maths/test/Fibonacci.test.js similarity index 97% rename from Maths/Fibonacci.test.js rename to Maths/test/Fibonacci.test.js index cad3443736..985705feb7 100644 --- a/Maths/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -3,7 +3,7 @@ import { FibonacciRecursiveDP, FibonacciIterative, FibonacciRecursive, -} from './Fibonacci' +} from '../Fibonacci' describe('Fibonanci', () => { it('should return an array of numbers for FibonnaciIterative', () => { diff --git a/Maths/FindHcf.test.js b/Maths/test/FindHcf.test.js similarity index 94% rename from Maths/FindHcf.test.js rename to Maths/test/FindHcf.test.js index d2bc9dc619..ccb5c30459 100644 --- a/Maths/FindHcf.test.js +++ b/Maths/test/FindHcf.test.js @@ -1,4 +1,4 @@ -import { findHCF } from './FindHcf' +import { findHCF } from '../FindHcf' describe('findHCF', () => { it('should throw a statement for values less than 1', () => { diff --git a/Maths/FindLcm.test.js b/Maths/test/FindLcm.test.js similarity index 94% rename from Maths/FindLcm.test.js rename to Maths/test/FindLcm.test.js index cfd0877ed8..1e5c0905cd 100644 --- a/Maths/FindLcm.test.js +++ b/Maths/test/FindLcm.test.js @@ -1,4 +1,4 @@ -import { findLcm } from './FindLcm' +import { findLcm } from '../FindLcm' describe('findLcm', () => { it('should throw a statement for values less than 1', () => { diff --git a/Maths/GridGet.test.js b/Maths/test/GridGet.test.js similarity index 93% rename from Maths/GridGet.test.js rename to Maths/test/GridGet.test.js index 2c3cfb1ead..eef51fc6f0 100644 --- a/Maths/GridGet.test.js +++ b/Maths/test/GridGet.test.js @@ -1,4 +1,4 @@ -import { gridGetX, gridGetY } from './GridGet' +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', () => { diff --git a/Maths/MeanSquareError.test.js b/Maths/test/MeanSquareError.test.js similarity index 92% rename from Maths/MeanSquareError.test.js rename to Maths/test/MeanSquareError.test.js index 7a19a5571f..ecd53de89a 100644 --- a/Maths/MeanSquareError.test.js +++ b/Maths/test/MeanSquareError.test.js @@ -1,4 +1,4 @@ -import { meanSquaredError } from './MeanSquareError' +import { meanSquaredError } from '../MeanSquareError' describe('meanSquareError', () => { it('should throw an error on non-array arguments', () => { diff --git a/Maths/ModularBinaryExponentiationRecursive.test.js b/Maths/test/ModularBinaryExponentiationRecursive.test.js similarity index 66% rename from Maths/ModularBinaryExponentiationRecursive.test.js rename to Maths/test/ModularBinaryExponentiationRecursive.test.js index 2b541a887f..9758d0ed1d 100644 --- a/Maths/ModularBinaryExponentiationRecursive.test.js +++ b/Maths/test/ModularBinaryExponentiationRecursive.test.js @@ -1,4 +1,4 @@ -import { modularBinaryExponentiation } from './ModularBinaryExponentiationRecursive' +import { modularBinaryExponentiation } from '../ModularBinaryExponentiationRecursive' describe('modularBinaryExponentiation', () => { it('should return the binary exponentiation', () => { diff --git a/Maths/Palindrome.test.js b/Maths/test/Palindrome.test.js similarity index 88% rename from Maths/Palindrome.test.js rename to Maths/test/Palindrome.test.js index 44717d4c60..53cb883955 100644 --- a/Maths/Palindrome.test.js +++ b/Maths/test/Palindrome.test.js @@ -1,4 +1,4 @@ -import { PalindromeRecursive, PalindromeIterative } from './Palindrome' +import { PalindromeRecursive, PalindromeIterative } from '../Palindrome' describe('Palindrome', () => { it('should return true for a palindrome for PalindromeRecursive', () => { diff --git a/Maths/PascalTriangle.test.js b/Maths/test/PascalTriangle.test.js similarity index 92% rename from Maths/PascalTriangle.test.js rename to Maths/test/PascalTriangle.test.js index 15c4455344..314d0f3211 100644 --- a/Maths/PascalTriangle.test.js +++ b/Maths/test/PascalTriangle.test.js @@ -1,4 +1,4 @@ -import { generate } from './PascalTriangle' +import { generate } from '../PascalTriangle' describe('Pascals Triangle', () => { it('should have the the same length as the number', () => { diff --git a/Maths/PiApproximationMonteCarlo.test.js b/Maths/test/PiApproximationMonteCarlo.test.js similarity index 77% rename from Maths/PiApproximationMonteCarlo.test.js rename to Maths/test/PiApproximationMonteCarlo.test.js index 5eef3d789d..9727aa5788 100644 --- a/Maths/PiApproximationMonteCarlo.test.js +++ b/Maths/test/PiApproximationMonteCarlo.test.js @@ -1,4 +1,4 @@ -import { piEstimation } from './PiApproximationMonteCarlo' +import { piEstimation } from '../PiApproximationMonteCarlo' describe('PiApproximationMonteCarlo', () => { it('should be between the range of 2 to 4', () => { diff --git a/Maths/Polynomial.test.js b/Maths/test/Polynomial.test.js similarity index 96% rename from Maths/Polynomial.test.js rename to Maths/test/Polynomial.test.js index ed7b305233..af5618ab3d 100644 --- a/Maths/Polynomial.test.js +++ b/Maths/test/Polynomial.test.js @@ -1,4 +1,4 @@ -import { Polynomial } from './Polynomial' +import { Polynomial } from '../Polynomial' describe('Polynomial', () => { it('should not return a expression for zero', () => { diff --git a/Maths/PrimeCheck.test.js b/Maths/test/PrimeCheck.test.js similarity index 89% rename from Maths/PrimeCheck.test.js rename to Maths/test/PrimeCheck.test.js index e7aafcaf57..da1cd1b52f 100644 --- a/Maths/PrimeCheck.test.js +++ b/Maths/test/PrimeCheck.test.js @@ -1,4 +1,4 @@ -import { PrimeCheck } from './PrimeCheck' +import { PrimeCheck } from '../PrimeCheck' describe('PrimeCheck', () => { it('should return true for Prime Numbers', () => { diff --git a/Maths/ReversePolishNotation.test.js b/Maths/test/ReversePolishNotation.test.js similarity index 85% rename from Maths/ReversePolishNotation.test.js rename to Maths/test/ReversePolishNotation.test.js index 91882b3b01..8b880ee472 100644 --- a/Maths/ReversePolishNotation.test.js +++ b/Maths/test/ReversePolishNotation.test.js @@ -1,4 +1,4 @@ -import { calcRPN } from './ReversePolishNotation' +import { calcRPN } from '../ReversePolishNotation' describe('ReversePolishNotation', () => { it('should evaluate correctly for two values', () => { diff --git a/Maths/SieveOfEratosthenes.test.js b/Maths/test/SieveOfEratosthenes.test.js similarity index 75% rename from Maths/SieveOfEratosthenes.test.js rename to Maths/test/SieveOfEratosthenes.test.js index 0f4de8ac45..8b51e2471c 100644 --- a/Maths/SieveOfEratosthenes.test.js +++ b/Maths/test/SieveOfEratosthenes.test.js @@ -1,5 +1,5 @@ -import { sieveOfEratosthenes } from './SieveOfEratosthenes' -import { PrimeCheck } from './PrimeCheck' +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', () => { diff --git a/String/CheckAnagram.test.js b/String/test/CheckAnagram.test.js similarity index 95% rename from String/CheckAnagram.test.js rename to String/test/CheckAnagram.test.js index ebb1ee5f9f..691d5ba892 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` 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 3bd401ba14..cfe88f7e53 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 23e892dd78..d0eab80b6f 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 96% rename from String/ReverseString.test.js rename to String/test/ReverseString.test.js index 1658c8bde6..dbc4502913 100644 --- a/String/ReverseString.test.js +++ b/String/test/ReverseString.test.js @@ -1,7 +1,7 @@ import { ReverseStringIterative, - ReverseStringIterativeInplace -} from './ReverseString' + ReverseStringIterativeInplace, +} 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 7c4aa16dd9..32bd9d462f 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', () => { From b1670b6e747cd94731080ba5e92cf7adc6e9bd04 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Tue, 6 Oct 2020 11:24:35 +0000 Subject: [PATCH 21/21] chore: fix standardjs errors --- .prettierrc | 2 +- Maths/AverageMean.js | 3 +-- Maths/Factorial.js | 4 ++-- Maths/Polynomial.js | 8 ++++---- Maths/ReversePolishNotation.js | 2 +- Maths/test/Fibonacci.test.js | 2 +- Maths/test/SieveOfEratosthenes.test.js | 2 +- Recursive/FibonacciNumberRecursive.js | 1 - String/test/ReverseString.test.js | 2 +- 9 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.prettierrc b/.prettierrc index d6688ba6f9..a85cb7eb8e 100644 --- a/.prettierrc +++ b/.prettierrc @@ -10,6 +10,6 @@ "semi": false, "singleQuote": true, "tabWidth": 2, - "trailingComma": "es5", + "trailingComma": "none", "useTabs": false } diff --git a/Maths/AverageMean.js b/Maths/AverageMean.js index 05963f995c..43f3f8ca00 100644 --- a/Maths/AverageMean.js +++ b/Maths/AverageMean.js @@ -15,7 +15,6 @@ const mean = (nums) => { // This is a function returns average/mean of array let sum = 0 - let avg // This loop sums all values in the 'nums' array using forEach loop nums.forEach(function (current) { @@ -23,7 +22,7 @@ const mean = (nums) => { }) // Divide sum by the length of the 'nums' array. - avg = sum / nums.length + const avg = sum / nums.length return avg } diff --git a/Maths/Factorial.js b/Maths/Factorial.js index f1c433886e..04d9ef08c2 100644 --- a/Maths/Factorial.js +++ b/Maths/Factorial.js @@ -16,7 +16,7 @@ const calcRange = (num) => { // Generate a range of numbers from 1 to `num`. let i = 1 - let range = [] + const range = [] while (i <= num) { range.push(i) i += 1 @@ -26,7 +26,7 @@ const calcRange = (num) => { const calcFactorial = (num) => { let factorial - let range = calcRange(num) + const range = calcRange(num) // Check if the number is negative, positive, null, undefined, or zero if (num < 0) { diff --git a/Maths/Polynomial.js b/Maths/Polynomial.js index 89c287bfc3..41ec340ee7 100644 --- a/Maths/Polynomial.js +++ b/Maths/Polynomial.js @@ -9,7 +9,7 @@ * The members of array are coefficients and their indexes as exponents. */ class Polynomial { - constructor(array) { + constructor (array) { this.coefficientArray = array // array of coefficients this.polynomial = '' // in terms of x e.g. (2x) + (1) this.construct() @@ -18,7 +18,7 @@ class Polynomial { /** * Function to construct the polynomial in terms of x using the coefficientArray */ - construct() { + construct () { this.polynomial = this.coefficientArray .map((coefficient, exponent) => { if (coefficient === 0) { @@ -45,7 +45,7 @@ class Polynomial { * Function to display polynomial in terms of x * @returns {String} of polynomial representation in terms of x */ - display() { + display () { return this.polynomial } @@ -53,7 +53,7 @@ class Polynomial { * Function to calculate the value of the polynomial by substituting variable x * @param {Number} value */ - evaluate(value) { + evaluate (value) { return this.coefficientArray.reduce((result, coefficient, exponent) => { return result + coefficient * Math.pow(value, exponent) }, 0) diff --git a/Maths/ReversePolishNotation.js b/Maths/ReversePolishNotation.js index 565db012b5..efe6240dc0 100644 --- a/Maths/ReversePolishNotation.js +++ b/Maths/ReversePolishNotation.js @@ -5,7 +5,7 @@ const calcRPN = (expression) => { '+': (a, b) => a + b, '-': (a, b) => a - b, '*': (a, b) => a * b, - '/': (a, b) => b / a, + '/': (a, b) => b / a } const tokens = expression.split(' ') diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 985705feb7..e5b9376f87 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -2,7 +2,7 @@ import { FibonacciDpWithoutRecursion, FibonacciRecursiveDP, FibonacciIterative, - FibonacciRecursive, + FibonacciRecursive } from '../Fibonacci' describe('Fibonanci', () => { diff --git a/Maths/test/SieveOfEratosthenes.test.js b/Maths/test/SieveOfEratosthenes.test.js index 8b51e2471c..056693d39b 100644 --- a/Maths/test/SieveOfEratosthenes.test.js +++ b/Maths/test/SieveOfEratosthenes.test.js @@ -7,7 +7,7 @@ describe('should return an array of prime booleans', () => { const primes = sieveOfEratosthenes(n) primes.forEach((primeBool, index) => { if (primeBool) { - expect(PrimeCheck(index)).toBeTruthy + expect(PrimeCheck(index)).toBeTruthy() } }) }) diff --git a/Recursive/FibonacciNumberRecursive.js b/Recursive/FibonacciNumberRecursive.js index 406c866846..e5c5bb95bb 100644 --- a/Recursive/FibonacciNumberRecursive.js +++ b/Recursive/FibonacciNumberRecursive.js @@ -6,7 +6,6 @@ const fibonacci = (N) => { return fibonacci(N - 2) + fibonacci(N - 1) } - // testing (() => { const number = 5 diff --git a/String/test/ReverseString.test.js b/String/test/ReverseString.test.js index dbc4502913..6ec1bb4dfd 100644 --- a/String/test/ReverseString.test.js +++ b/String/test/ReverseString.test.js @@ -1,6 +1,6 @@ import { ReverseStringIterative, - ReverseStringIterativeInplace, + ReverseStringIterativeInplace } from '../ReverseString' describe('ReverseStringIterative', () => {