mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Add tests to Math (#423)
* Add prettier config * test: add test to check for absolute function * chore: es5 to es6 * test: add test to check mean function * test: add test for sum of digit * test: add test for factorial * test: add test for fibonnaci * test: add test for find HCF * test: add test for lcm * test: add gridget test * test: add test for mean square error * test: add test for modular binary exponentiation * test: add tests for palindrome * test: add test for pascals triangle * test: add tests for polynomial * test: add tests for prime check * test: add tests for reverse polish notation * test: add tests for sieve of eratosthenes * test: add tests for pi estimation monte carlo method * chore: move tests to test folder * chore: fix standardjs errors
This commit is contained in:

committed by
GitHub

parent
554abf7126
commit
e112434dee
15
.prettierrc
Normal file
15
.prettierrc
Normal file
@ -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
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
https://en.wikipedia.org/wiki/Absolute_value
|
https://en.wikipedia.org/wiki/Absolute_value
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function absVal (num) {
|
const absVal = (num) => {
|
||||||
// Find absolute value of `num`.
|
// Find absolute value of `num`.
|
||||||
'use strict'
|
'use strict'
|
||||||
if (num < 0) {
|
if (num < 0) {
|
||||||
@ -21,6 +21,4 @@ function absVal (num) {
|
|||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run `abs` function to find absolute value of two numbers.
|
export { absVal }
|
||||||
console.log('The absolute value of -34 is ' + absVal(-34))
|
|
||||||
console.log('The absolute value of 34 is ' + absVal(34))
|
|
||||||
|
@ -14,8 +14,7 @@
|
|||||||
|
|
||||||
const mean = (nums) => {
|
const mean = (nums) => {
|
||||||
// This is a function returns average/mean of array
|
// This is a function returns average/mean of array
|
||||||
var sum = 0
|
let sum = 0
|
||||||
var avg
|
|
||||||
|
|
||||||
// This loop sums all values in the 'nums' array using forEach loop
|
// This loop sums all values in the 'nums' array using forEach loop
|
||||||
nums.forEach(function (current) {
|
nums.forEach(function (current) {
|
||||||
@ -23,9 +22,8 @@ const mean = (nums) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Divide sum by the length of the 'nums' array.
|
// Divide sum by the length of the 'nums' array.
|
||||||
avg = sum / nums.length
|
const avg = sum / nums.length
|
||||||
return avg
|
return avg
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run `mean` Function to find average of a list of numbers.
|
export { mean }
|
||||||
console.log(mean([2, 4, 6, 8, 20, 50, 70]))
|
|
||||||
|
@ -1,18 +1,16 @@
|
|||||||
// program to find sum of digits of a number
|
// program to find sum of digits of a number
|
||||||
|
|
||||||
// function which would calculate sum and return it
|
// function which would calculate sum and return it
|
||||||
function digitSum (num) {
|
const digitSum = (num) => {
|
||||||
// sum will store sum of digits of a number
|
// sum will store sum of digits of a number
|
||||||
let sum = 0
|
let sum = 0
|
||||||
// while will run untill num become 0
|
// while will run untill num become 0
|
||||||
while (num) {
|
while (num) {
|
||||||
sum += (num % 10)
|
sum += num % 10
|
||||||
num = parseInt(num / 10)
|
num = parseInt(num / 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
return sum
|
return sum
|
||||||
}
|
}
|
||||||
|
|
||||||
// assigning number
|
export { digitSum }
|
||||||
const num = 12345
|
|
||||||
console.log(digitSum(num))
|
|
@ -13,10 +13,10 @@
|
|||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
function calcRange (num) {
|
const calcRange = (num) => {
|
||||||
// Generate a range of numbers from 1 to `num`.
|
// Generate a range of numbers from 1 to `num`.
|
||||||
var i = 1
|
let i = 1
|
||||||
var range = []
|
const range = []
|
||||||
while (i <= num) {
|
while (i <= num) {
|
||||||
range.push(i)
|
range.push(i)
|
||||||
i += 1
|
i += 1
|
||||||
@ -24,9 +24,9 @@ function calcRange (num) {
|
|||||||
return range
|
return range
|
||||||
}
|
}
|
||||||
|
|
||||||
function calcFactorial (num) {
|
const calcFactorial = (num) => {
|
||||||
var factorial
|
let factorial
|
||||||
var range = calcRange(num)
|
const range = calcRange(num)
|
||||||
|
|
||||||
// Check if the number is negative, positive, null, undefined, or zero
|
// Check if the number is negative, positive, null, undefined, or zero
|
||||||
if (num < 0) {
|
if (num < 0) {
|
||||||
@ -43,11 +43,8 @@ function calcFactorial (num) {
|
|||||||
range.forEach(function (i) {
|
range.forEach(function (i) {
|
||||||
factorial = factorial * 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.
|
export { calcFactorial }
|
||||||
|
|
||||||
var num = console.log('Enter a number: ')
|
|
||||||
console.log(calcFactorial(num))
|
|
||||||
|
@ -66,18 +66,10 @@ const FibonacciDpWithoutRecursion = (number) => {
|
|||||||
for (var i = 2; i < number; ++i) {
|
for (var i = 2; i < number; ++i) {
|
||||||
table.push(table[i - 1] + table[i - 2])
|
table.push(table[i - 1] + table[i - 2])
|
||||||
}
|
}
|
||||||
return (table)
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
export { FibonacciDpWithoutRecursion }
|
||||||
|
export { FibonacciIterative }
|
||||||
console.log(FibonacciIterative(5))
|
export { FibonacciRecursive }
|
||||||
// Output: [ 1, 1, 2, 3, 5 ]
|
export { FibonacciRecursiveDP }
|
||||||
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 ]
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
https://en.wikipedia.org/wiki/Greatest_common_divisor
|
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 the input numbers are less than 1 return an error message.
|
||||||
if (x < 1 || y < 1) {
|
if (x < 1 || y < 1) {
|
||||||
return 'Please enter values greater than zero.'
|
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.
|
// When the while loop finishes the minimum of x and y is the HCF.
|
||||||
return Math.min(x, y)
|
return Math.min(x, y)
|
||||||
}
|
}
|
||||||
console.log(findHCF(27, 36))
|
|
||||||
|
export { findHCF }
|
||||||
|
@ -12,9 +12,19 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
// Find the LCM of two numbers.
|
// Find the LCM of two numbers.
|
||||||
function findLcm (num1, num2) {
|
const findLcm = (num1, num2) => {
|
||||||
var maxNum
|
// If the input numbers are less than 1 return an error message.
|
||||||
var lcm
|
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.
|
// Check to see whether num1 or num2 is larger.
|
||||||
if (num1 > num2) {
|
if (num1 > num2) {
|
||||||
maxNum = num1
|
maxNum = num1
|
||||||
@ -24,15 +34,10 @@ function findLcm (num1, num2) {
|
|||||||
lcm = maxNum
|
lcm = maxNum
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
|
if (lcm % num1 === 0 && lcm % num2 === 0) break
|
||||||
break
|
|
||||||
}
|
|
||||||
lcm += maxNum
|
lcm += maxNum
|
||||||
}
|
}
|
||||||
return lcm
|
return lcm
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run `findLcm` Function
|
export { findLcm }
|
||||||
var num1 = 12
|
|
||||||
var num2 = 76
|
|
||||||
console.log(findLcm(num1, num2))
|
|
||||||
|
@ -40,17 +40,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const gridGetX = (columns, index) => {
|
const gridGetX = (columns, index) => {
|
||||||
while ((index + 1) > columns) {
|
while (index + 1 > columns) {
|
||||||
index = index - columns
|
index = index - columns
|
||||||
}
|
}
|
||||||
return (index + 1)
|
return index + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
const gridGetY = (columns, index) => {
|
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)}`)
|
export { gridGetX, gridGetY }
|
||||||
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)}`)
|
|
||||||
|
@ -18,9 +18,4 @@ const meanSquaredError = (predicted, expected) => {
|
|||||||
return err / expected.length
|
return err / expected.length
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
export { meanSquaredError }
|
||||||
(() => {
|
|
||||||
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)
|
|
||||||
})()
|
|
||||||
|
@ -19,13 +19,4 @@ const modularBinaryExponentiation = (a, n, m) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { modularBinaryExponentiation }
|
||||||
// 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()
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
* @complexity: O(n)
|
* @complexity: O(n)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function PalindromeRecursive (string) {
|
const PalindromeRecursive = (string) => {
|
||||||
// Base case
|
// Base case
|
||||||
if (string.length < 2) return true
|
if (string.length < 2) return true
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ function PalindromeRecursive (string) {
|
|||||||
return PalindromeRecursive(string.slice(1, string.length - 1))
|
return PalindromeRecursive(string.slice(1, string.length - 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
function PalindromeIterative (string) {
|
const PalindromeIterative = (string) => {
|
||||||
const _string = string
|
const _string = string
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.replace(/ /g, '')
|
.replace(/ /g, '')
|
||||||
@ -45,7 +45,4 @@ function PalindromeIterative (string) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// testing
|
export { PalindromeIterative, PalindromeRecursive }
|
||||||
|
|
||||||
console.log(PalindromeRecursive('Javascript Community'))
|
|
||||||
console.log(PalindromeIterative('mom'))
|
|
||||||
|
@ -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]]
|
const triangle = [[1], [1, 1]]
|
||||||
|
|
||||||
if (numRows === 0) {
|
if (numRows === 0) {
|
||||||
@ -16,16 +26,5 @@ var generate = function (numRows) {
|
|||||||
}
|
}
|
||||||
return triangle
|
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 }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method
|
// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method
|
||||||
// Video Explaination: https://www.youtube.com/watch?v=ELetCV_wX_c
|
// Video Explaination: https://www.youtube.com/watch?v=ELetCV_wX_c
|
||||||
|
|
||||||
function piEstimation (iterations = 100000) {
|
const piEstimation = (iterations = 100000) => {
|
||||||
let circleCounter = 0
|
let circleCounter = 0
|
||||||
|
|
||||||
for (let i = 0; i < iterations; i++) {
|
for (let i = 0; i < iterations; i++) {
|
||||||
@ -18,8 +18,4 @@ function piEstimation (iterations = 100000) {
|
|||||||
return pi
|
return pi
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { piEstimation }
|
||||||
console.log(piEstimation())
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
|
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
|
||||||
* Terms of a polynomial are:
|
* Terms of a polynomial are:
|
||||||
@ -20,7 +19,8 @@ class Polynomial {
|
|||||||
* Function to construct the polynomial in terms of x using the coefficientArray
|
* Function to construct the polynomial in terms of x using the coefficientArray
|
||||||
*/
|
*/
|
||||||
construct () {
|
construct () {
|
||||||
this.polynomial = this.coefficientArray.map((coefficient, exponent) => {
|
this.polynomial = this.coefficientArray
|
||||||
|
.map((coefficient, exponent) => {
|
||||||
if (coefficient === 0) {
|
if (coefficient === 0) {
|
||||||
return '0'
|
return '0'
|
||||||
}
|
}
|
||||||
@ -55,28 +55,9 @@ class Polynomial {
|
|||||||
*/
|
*/
|
||||||
evaluate (value) {
|
evaluate (value) {
|
||||||
return this.coefficientArray.reduce((result, coefficient, exponent) => {
|
return this.coefficientArray.reduce((result, coefficient, exponent) => {
|
||||||
return result + coefficient * (Math.pow(value, exponent))
|
return result + coefficient * Math.pow(value, exponent)
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export { Polynomial }
|
||||||
* 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()
|
|
||||||
|
@ -9,6 +9,9 @@
|
|||||||
const PrimeCheck = (n) => {
|
const PrimeCheck = (n) => {
|
||||||
// input: n: int
|
// input: n: int
|
||||||
// output: boolean
|
// output: boolean
|
||||||
|
if (n === 1) return false
|
||||||
|
if (n === 0) return false
|
||||||
|
|
||||||
for (let i = 2; i * i <= n; i++) {
|
for (let i = 2; i * i <= n; i++) {
|
||||||
if (n % i === 0) {
|
if (n % i === 0) {
|
||||||
return false
|
return false
|
||||||
@ -17,13 +20,4 @@ const PrimeCheck = (n) => {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const main = () => {
|
export { PrimeCheck }
|
||||||
// PrimeCheck(1000003)
|
|
||||||
// > true
|
|
||||||
console.log(PrimeCheck(1000003))
|
|
||||||
// PrimeCheck(1000001)
|
|
||||||
// > false
|
|
||||||
console.log(PrimeCheck(1000001))
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// Wikipedia: https://en.wikipedia.org/wiki/Reverse_Polish_notation
|
// Wikipedia: https://en.wikipedia.org/wiki/Reverse_Polish_notation
|
||||||
|
|
||||||
function calcRPN (expression) {
|
const calcRPN = (expression) => {
|
||||||
const operators = {
|
const operators = {
|
||||||
'+': (a, b) => a + b,
|
'+': (a, b) => a + b,
|
||||||
'-': (a, b) => a - b,
|
'-': (a, b) => a - b,
|
||||||
@ -12,7 +12,7 @@ function calcRPN (expression) {
|
|||||||
|
|
||||||
const stack = []
|
const stack = []
|
||||||
|
|
||||||
tokens.forEach(token => {
|
tokens.forEach((token) => {
|
||||||
const operator = operators[token]
|
const operator = operators[token]
|
||||||
|
|
||||||
if (typeof operator === 'function') {
|
if (typeof operator === 'function') {
|
||||||
@ -30,6 +30,4 @@ function calcRPN (expression) {
|
|||||||
return stack.pop()
|
return stack.pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(calcRPN('2 2 2 * +') === 6)
|
export { calcRPN }
|
||||||
console.log(calcRPN('2 2 + 2 *') === 8)
|
|
||||||
console.log(calcRPN('6 9 7 + 2 / + 3 *') === 42)
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
function sieveOfEratosthenes (n) {
|
const sieveOfEratosthenes = (n) => {
|
||||||
/*
|
/*
|
||||||
* Calculates prime numbers till a number n
|
* Calculates prime numbers till a number n
|
||||||
* :param n: Number upto which to calculate primes
|
* :param n: Number upto which to calculate primes
|
||||||
@ -18,14 +18,4 @@ function sieveOfEratosthenes (n) {
|
|||||||
return primes
|
return primes
|
||||||
}
|
}
|
||||||
|
|
||||||
function main () {
|
export { sieveOfEratosthenes }
|
||||||
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()
|
|
||||||
|
13
Maths/test/Abs.test.js
Normal file
13
Maths/test/Abs.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
11
Maths/test/AverageMean.test.js
Normal file
11
Maths/test/AverageMean.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
11
Maths/test/DigitSum.test.js
Normal file
11
Maths/test/DigitSum.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
35
Maths/test/Factorial.test.js
Normal file
35
Maths/test/Factorial.test.js
Normal file
@ -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')
|
||||||
|
})
|
||||||
|
})
|
30
Maths/test/Fibonacci.test.js
Normal file
30
Maths/test/Fibonacci.test.js
Normal file
@ -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])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
20
Maths/test/FindHcf.test.js
Normal file
20
Maths/test/FindHcf.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
20
Maths/test/FindLcm.test.js
Normal file
20
Maths/test/FindLcm.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
16
Maths/test/GridGet.test.js
Normal file
16
Maths/test/GridGet.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
21
Maths/test/MeanSquareError.test.js
Normal file
21
Maths/test/MeanSquareError.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
7
Maths/test/ModularBinaryExponentiationRecursive.test.js
Normal file
7
Maths/test/ModularBinaryExponentiationRecursive.test.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { modularBinaryExponentiation } from '../ModularBinaryExponentiationRecursive'
|
||||||
|
|
||||||
|
describe('modularBinaryExponentiation', () => {
|
||||||
|
it('should return the binary exponentiation', () => {
|
||||||
|
expect(modularBinaryExponentiation(2, 10, 17)).toBe(4)
|
||||||
|
})
|
||||||
|
})
|
16
Maths/test/Palindrome.test.js
Normal file
16
Maths/test/Palindrome.test.js
Normal file
@ -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()
|
||||||
|
})
|
||||||
|
})
|
20
Maths/test/PascalTriangle.test.js
Normal file
20
Maths/test/PascalTriangle.test.js
Normal file
@ -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]])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
9
Maths/test/PiApproximationMonteCarlo.test.js
Normal file
9
Maths/test/PiApproximationMonteCarlo.test.js
Normal file
@ -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()
|
||||||
|
})
|
||||||
|
})
|
37
Maths/test/Polynomial.test.js
Normal file
37
Maths/test/Polynomial.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
14
Maths/test/PrimeCheck.test.js
Normal file
14
Maths/test/PrimeCheck.test.js
Normal file
@ -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()
|
||||||
|
})
|
||||||
|
})
|
11
Maths/test/ReversePolishNotation.test.js
Normal file
11
Maths/test/ReversePolishNotation.test.js
Normal file
@ -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)
|
||||||
|
})
|
||||||
|
})
|
14
Maths/test/SieveOfEratosthenes.test.js
Normal file
14
Maths/test/SieveOfEratosthenes.test.js
Normal file
@ -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()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
@ -24,7 +24,6 @@ const checkIfPatternExists = (text, pattern) => {
|
|||||||
// For each iteration of j check if the value of
|
// For each iteration of j check if the value of
|
||||||
// j + 1 is equal to the length of the pattern
|
// j + 1 is equal to the length of the pattern
|
||||||
if (j + 1 === patternLength) {
|
if (j + 1 === patternLength) {
|
||||||
console.log(`Given pattern is found at index ${i}`)
|
|
||||||
return `Given pattern is found at index ${i}`
|
return `Given pattern is found at index ${i}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { checkAnagram } from './CheckAnagram'
|
import { checkAnagram } from '../CheckAnagram'
|
||||||
|
|
||||||
describe('checkAnagram', () => {
|
describe('checkAnagram', () => {
|
||||||
it.each`
|
it.each`
|
||||||
@ -18,7 +18,7 @@ describe('checkAnagram', () => {
|
|||||||
)
|
)
|
||||||
it('expects to return "Not anagram" if the arguments have different lengths', () => {
|
it('expects to return "Not anagram" if the arguments have different lengths', () => {
|
||||||
const SUT = checkAnagram('abs', 'abds')
|
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', () => {
|
it('expects to return "Not anagram" if the arguments are not anagrams', () => {
|
||||||
const SUT = checkAnagram('abcs', 'abds')
|
const SUT = checkAnagram('abcs', 'abds')
|
@ -1,4 +1,4 @@
|
|||||||
import { checkPalindrome } from './CheckPalindrome'
|
import { checkPalindrome } from '../CheckPalindrome'
|
||||||
|
|
||||||
describe('checkPalindrome', () => {
|
describe('checkPalindrome', () => {
|
||||||
it('expects to return "Palindrome" if the given string is a palindrome', () => {
|
it('expects to return "Palindrome" if the given string is a palindrome', () => {
|
@ -1,4 +1,4 @@
|
|||||||
import { checkIfPatternExists } from './PatternMatching'
|
import { checkIfPatternExists } from '../PatternMatching'
|
||||||
describe('checkIfPatternExists', () => {
|
describe('checkIfPatternExists', () => {
|
||||||
it('expects to find a pattern with correct input', () => {
|
it('expects to find a pattern with correct input', () => {
|
||||||
const text = 'AABAACAADAABAAAABAA'
|
const text = 'AABAACAADAABAAAABAA'
|
||||||
@ -21,6 +21,8 @@ describe('checkIfPatternExists', () => {
|
|||||||
it('expects to throw an error message when given inpuut is not a string', () => {
|
it('expects to throw an error message when given inpuut is not a string', () => {
|
||||||
const text = 123444456
|
const text = 123444456
|
||||||
const pattern = 123
|
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'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
@ -1,7 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
ReverseStringIterative,
|
ReverseStringIterative,
|
||||||
ReverseStringIterativeInplace
|
ReverseStringIterativeInplace
|
||||||
} from './ReverseString'
|
} from '../ReverseString'
|
||||||
|
|
||||||
describe('ReverseStringIterative', () => {
|
describe('ReverseStringIterative', () => {
|
||||||
it('expects to reverse a simple string', () => {
|
it('expects to reverse a simple string', () => {
|
@ -1,4 +1,4 @@
|
|||||||
import { reverseWords } from './ReverseWords'
|
import { reverseWords } from '../ReverseWords'
|
||||||
|
|
||||||
describe('reverseWords', () => {
|
describe('reverseWords', () => {
|
||||||
it('expects to reverse words to return a joined word', () => {
|
it('expects to reverse words to return a joined word', () => {
|
Reference in New Issue
Block a user