Skip to content

Commit e112434

Browse files
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
1 parent 554abf7 commit e112434

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+420
-172
lines changed

Diff for: .prettierrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"endOfLine": "lf",
5+
"insertPragma": false,
6+
"printWidth": 80,
7+
"proseWrap": "preserve",
8+
"quoteProps": "as-needed",
9+
"requirePragma": false,
10+
"semi": false,
11+
"singleQuote": true,
12+
"tabWidth": 2,
13+
"trailingComma": "none",
14+
"useTabs": false
15+
}

Diff for: Maths/Abs.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
https://en.wikipedia.org/wiki/Absolute_value
1212
*/
1313

14-
function absVal (num) {
14+
const absVal = (num) => {
1515
// Find absolute value of `num`.
1616
'use strict'
1717
if (num < 0) {
@@ -21,6 +21,4 @@ function absVal (num) {
2121
return num
2222
}
2323

24-
// Run `abs` function to find absolute value of two numbers.
25-
console.log('The absolute value of -34 is ' + absVal(-34))
26-
console.log('The absolute value of 34 is ' + absVal(34))
24+
export { absVal }

Diff for: Maths/AverageMean.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@
1414

1515
const mean = (nums) => {
1616
// This is a function returns average/mean of array
17-
var sum = 0
18-
var avg
17+
let sum = 0
1918

2019
// This loop sums all values in the 'nums' array using forEach loop
2120
nums.forEach(function (current) {
2221
sum += current
2322
})
2423

2524
// Divide sum by the length of the 'nums' array.
26-
avg = sum / nums.length
25+
const avg = sum / nums.length
2726
return avg
2827
}
2928

30-
// Run `mean` Function to find average of a list of numbers.
31-
console.log(mean([2, 4, 6, 8, 20, 50, 70]))
29+
export { mean }

Diff for: Maths/digitSum.js renamed to Maths/DigitSum.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
// program to find sum of digits of a number
22

33
// function which would calculate sum and return it
4-
function digitSum (num) {
4+
const digitSum = (num) => {
55
// sum will store sum of digits of a number
66
let sum = 0
77
// while will run untill num become 0
88
while (num) {
9-
sum += (num % 10)
9+
sum += num % 10
1010
num = parseInt(num / 10)
1111
}
1212

1313
return sum
1414
}
1515

16-
// assigning number
17-
const num = 12345
18-
console.log(digitSum(num))
16+
export { digitSum }

Diff for: Maths/Factorial.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313

1414
'use strict'
1515

16-
function calcRange (num) {
16+
const calcRange = (num) => {
1717
// Generate a range of numbers from 1 to `num`.
18-
var i = 1
19-
var range = []
18+
let i = 1
19+
const range = []
2020
while (i <= num) {
2121
range.push(i)
2222
i += 1
2323
}
2424
return range
2525
}
2626

27-
function calcFactorial (num) {
28-
var factorial
29-
var range = calcRange(num)
27+
const calcFactorial = (num) => {
28+
let factorial
29+
const range = calcRange(num)
3030

3131
// Check if the number is negative, positive, null, undefined, or zero
3232
if (num < 0) {
@@ -43,11 +43,8 @@ function calcFactorial (num) {
4343
range.forEach(function (i) {
4444
factorial = factorial * i
4545
})
46-
return 'The factorial of ' + num + ' is ' + factorial
46+
return `The factorial of ${num} is ${factorial}`
4747
}
4848
}
4949

50-
// Run `factorial` Function to find average of a list of numbers.
51-
52-
var num = console.log('Enter a number: ')
53-
console.log(calcFactorial(num))
50+
export { calcFactorial }

Diff for: Maths/Fibonacci.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,10 @@ const FibonacciDpWithoutRecursion = (number) => {
6666
for (var i = 2; i < number; ++i) {
6767
table.push(table[i - 1] + table[i - 2])
6868
}
69-
return (table)
69+
return table
7070
}
7171

72-
// testing
73-
74-
console.log(FibonacciIterative(5))
75-
// Output: [ 1, 1, 2, 3, 5 ]
76-
console.log(FibonacciRecursive(5))
77-
// Output: [ 1, 1, 2, 3, 5 ]
78-
79-
console.log(FibonacciRecursiveDP(5))
80-
// Output: 5
81-
82-
console.log(FibonacciDpWithoutRecursion(5))
83-
// Output: [ 1, 1, 2, 3, 5 ]
72+
export { FibonacciDpWithoutRecursion }
73+
export { FibonacciIterative }
74+
export { FibonacciRecursive }
75+
export { FibonacciRecursiveDP }

Diff for: Maths/FindHcf.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
https://en.wikipedia.org/wiki/Greatest_common_divisor
55
*/
66

7-
function findHCF (x, y) {
7+
const findHCF = (x, y) => {
88
// If the input numbers are less than 1 return an error message.
99
if (x < 1 || y < 1) {
1010
return 'Please enter values greater than zero.'
@@ -27,4 +27,5 @@ function findHCF (x, y) {
2727
// When the while loop finishes the minimum of x and y is the HCF.
2828
return Math.min(x, y)
2929
}
30-
console.log(findHCF(27, 36))
30+
31+
export { findHCF }

Diff for: Maths/FindLcm.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@
1212
'use strict'
1313

1414
// Find the LCM of two numbers.
15-
function findLcm (num1, num2) {
16-
var maxNum
17-
var lcm
15+
const findLcm = (num1, num2) => {
16+
// If the input numbers are less than 1 return an error message.
17+
if (num1 < 1 || num2 < 1) {
18+
return 'Please enter values greater than zero.'
19+
}
20+
21+
// If the input numbers are not integers return an error message.
22+
if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) {
23+
return 'Please enter whole numbers.'
24+
}
25+
26+
let maxNum
27+
let lcm
1828
// Check to see whether num1 or num2 is larger.
1929
if (num1 > num2) {
2030
maxNum = num1
@@ -24,15 +34,10 @@ function findLcm (num1, num2) {
2434
lcm = maxNum
2535

2636
while (true) {
27-
if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
28-
break
29-
}
37+
if (lcm % num1 === 0 && lcm % num2 === 0) break
3038
lcm += maxNum
3139
}
3240
return lcm
3341
}
3442

35-
// Run `findLcm` Function
36-
var num1 = 12
37-
var num2 = 76
38-
console.log(findLcm(num1, num2))
43+
export { findLcm }

Diff for: Maths/GridGet.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,14 @@
4040
*/
4141

4242
const gridGetX = (columns, index) => {
43-
while ((index + 1) > columns) {
43+
while (index + 1 > columns) {
4444
index = index - columns
4545
}
46-
return (index + 1)
46+
return index + 1
4747
}
4848

4949
const gridGetY = (columns, index) => {
50-
return (Math.floor(index / columns)) + 1
50+
return Math.floor(index / columns) + 1
5151
}
5252

53-
console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400), 27)}`)
54-
console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7, 11)}`)
55-
console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400), 27)}`)
56-
console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7, 11)}`)
53+
export { gridGetX, gridGetY }

Diff for: Maths/MeanSquareError.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@ const meanSquaredError = (predicted, expected) => {
1818
return err / expected.length
1919
}
2020

21-
// testing
22-
(() => {
23-
console.log(meanSquaredError([1, 2, 3, 4], [1, 2, 3, 4]) === 0)
24-
console.log(meanSquaredError([4, 3, 2, 1], [1, 2, 3, 4]) === 5)
25-
console.log(meanSquaredError([2, 0, 2, 0], [0, 0, 0, 0]) === 3)
26-
})()
21+
export { meanSquaredError }

Diff for: Maths/ModularBinaryExponentiationRecursive.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,4 @@ const modularBinaryExponentiation = (a, n, m) => {
1919
}
2020
}
2121

22-
const main = () => {
23-
// binary_exponentiation(2, 10, 17)
24-
// > 4
25-
console.log(modularBinaryExponentiation(2, 10, 17))
26-
// binary_exponentiation(3, 9, 12)
27-
// > 3
28-
console.log(modularBinaryExponentiation(3, 9, 12))
29-
}
30-
31-
main()
22+
export { modularBinaryExponentiation }

Diff for: Maths/Palindrome.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @complexity: O(n)
1515
*/
1616

17-
function PalindromeRecursive (string) {
17+
const PalindromeRecursive = (string) => {
1818
// Base case
1919
if (string.length < 2) return true
2020

@@ -26,7 +26,7 @@ function PalindromeRecursive (string) {
2626
return PalindromeRecursive(string.slice(1, string.length - 1))
2727
}
2828

29-
function PalindromeIterative (string) {
29+
const PalindromeIterative = (string) => {
3030
const _string = string
3131
.toLowerCase()
3232
.replace(/ /g, '')
@@ -45,7 +45,4 @@ function PalindromeIterative (string) {
4545
return true
4646
}
4747

48-
// testing
49-
50-
console.log(PalindromeRecursive('Javascript Community'))
51-
console.log(PalindromeIterative('mom'))
48+
export { PalindromeIterative, PalindromeRecursive }

Diff for: Maths/PascalTriangle.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
const numRows = 5
1+
const addRow = (triangle) => {
2+
const previous = triangle[triangle.length - 1]
3+
const newRow = [1]
4+
for (let i = 0; i < previous.length - 1; i++) {
5+
const current = previous[i]
6+
const next = previous[i + 1]
7+
newRow.push(current + next)
8+
}
9+
newRow.push(1)
10+
return triangle.push(newRow)
11+
}
212

3-
var generate = function (numRows) {
13+
const generate = (numRows) => {
414
const triangle = [[1], [1, 1]]
515

616
if (numRows === 0) {
@@ -16,16 +26,5 @@ var generate = function (numRows) {
1626
}
1727
return triangle
1828
}
19-
var addRow = function (triangle) {
20-
const previous = triangle[triangle.length - 1]
21-
const newRow = [1]
22-
for (let i = 0; i < previous.length - 1; i++) {
23-
const current = previous[i]
24-
const next = previous[i + 1]
25-
newRow.push(current + next)
26-
}
27-
newRow.push(1)
28-
return triangle.push(newRow)
29-
}
3029

31-
generate(numRows)
30+
export { generate }

Diff for: Maths/PiApproximationMonteCarlo.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Wikipedia: https://en.wikipedia.org/wiki/Monte_Carlo_method
22
// Video Explaination: https://www.youtube.com/watch?v=ELetCV_wX_c
33

4-
function piEstimation (iterations = 100000) {
4+
const piEstimation = (iterations = 100000) => {
55
let circleCounter = 0
66

77
for (let i = 0; i < iterations; i++) {
@@ -18,8 +18,4 @@ function piEstimation (iterations = 100000) {
1818
return pi
1919
}
2020

21-
function main () {
22-
console.log(piEstimation())
23-
}
24-
25-
main()
21+
export { piEstimation }

Diff for: Maths/Polynomial.js

+15-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
43
* Terms of a polynomial are:
@@ -20,18 +19,19 @@ class Polynomial {
2019
* Function to construct the polynomial in terms of x using the coefficientArray
2120
*/
2221
construct () {
23-
this.polynomial = this.coefficientArray.map((coefficient, exponent) => {
24-
if (coefficient === 0) {
25-
return '0'
26-
}
27-
if (exponent === 0) {
28-
return `(${coefficient})`
29-
} else if (exponent === 1) {
30-
return `(${coefficient}x)`
31-
} else {
32-
return `(${coefficient}x^${exponent})`
33-
}
34-
})
22+
this.polynomial = this.coefficientArray
23+
.map((coefficient, exponent) => {
24+
if (coefficient === 0) {
25+
return '0'
26+
}
27+
if (exponent === 0) {
28+
return `(${coefficient})`
29+
} else if (exponent === 1) {
30+
return `(${coefficient}x)`
31+
} else {
32+
return `(${coefficient}x^${exponent})`
33+
}
34+
})
3535
.filter((x) => {
3636
if (x !== '0') {
3737
return x
@@ -55,28 +55,9 @@ class Polynomial {
5555
*/
5656
evaluate (value) {
5757
return this.coefficientArray.reduce((result, coefficient, exponent) => {
58-
return result + coefficient * (Math.pow(value, exponent))
58+
return result + coefficient * Math.pow(value, exponent)
5959
}, 0)
6060
}
6161
}
6262

63-
/**
64-
* Function to perform tests
65-
*/
66-
const tests = () => {
67-
const polynomialOne = new Polynomial([1, 2, 3, 4])
68-
console.log('Test 1: [1,2,3,4]')
69-
console.log('Display Polynomial ', polynomialOne.display())
70-
// (4x^3) + (3x^2) + (2x) + (1)
71-
console.log('Evaluate Polynomial value=2 ', polynomialOne.evaluate(2))
72-
// 49
73-
74-
const polynomialTwo = new Polynomial([5, 0, 0, -4, 3])
75-
console.log('Test 2: [5,0,0,-4,3]')
76-
console.log('Display Polynomial ', polynomialTwo.display())
77-
// (3x^4) + (-4x^3) + (5)
78-
console.log('Evaluate Polynomial value=1 ', polynomialTwo.evaluate(1))
79-
// 4
80-
}
81-
82-
tests()
63+
export { Polynomial }

0 commit comments

Comments
 (0)