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

.prettierrc

Lines changed: 15 additions & 0 deletions
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+
}

Maths/Abs.js

Lines changed: 2 additions & 4 deletions
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 }

Maths/AverageMean.js

Lines changed: 3 additions & 5 deletions
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 }
Lines changed: 3 additions & 5 deletions
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 }

Maths/Factorial.js

Lines changed: 8 additions & 11 deletions
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 }

Maths/Fibonacci.js

Lines changed: 5 additions & 13 deletions
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 }

Maths/FindHcf.js

Lines changed: 3 additions & 2 deletions
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 }

Maths/FindLcm.js

Lines changed: 15 additions & 10 deletions
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 }

Maths/GridGet.js

Lines changed: 4 additions & 7 deletions
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 }

Maths/MeanSquareError.js

Lines changed: 1 addition & 6 deletions
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 }

0 commit comments

Comments
 (0)