Skip to content

Add tests to Math #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 2 additions & 4 deletions Maths/Abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }
8 changes: 3 additions & 5 deletions Maths/AverageMean.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@

const mean = (nums) => {
// This is a function returns average/mean of array
var sum = 0
var avg
let sum = 0

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

// Divide sum by the length of the 'nums' array.
avg = sum / nums.length
const avg = sum / nums.length
return avg
}

// Run `mean` Function to find average of a list of numbers.
console.log(mean([2, 4, 6, 8, 20, 50, 70]))
export { mean }
8 changes: 3 additions & 5 deletions Maths/digitSum.js → Maths/DigitSum.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// program to find sum of digits of a number

// function which would calculate sum and return it
function digitSum (num) {
const digitSum = (num) => {
// sum will store sum of digits of a number
let sum = 0
// while will run untill num become 0
while (num) {
sum += (num % 10)
sum += num % 10
num = parseInt(num / 10)
}

return sum
}

// assigning number
const num = 12345
console.log(digitSum(num))
export { digitSum }
19 changes: 8 additions & 11 deletions Maths/Factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@

'use strict'

function calcRange (num) {
const calcRange = (num) => {
// Generate a range of numbers from 1 to `num`.
var i = 1
var range = []
let i = 1
const range = []
while (i <= num) {
range.push(i)
i += 1
}
return range
}

function calcFactorial (num) {
var factorial
var range = calcRange(num)
const calcFactorial = (num) => {
let factorial
const range = calcRange(num)

// Check if the number is negative, positive, null, undefined, or zero
if (num < 0) {
Expand All @@ -43,11 +43,8 @@ function calcFactorial (num) {
range.forEach(function (i) {
factorial = factorial * i
})
return 'The factorial of ' + num + ' is ' + factorial
return `The factorial of ${num} is ${factorial}`
}
}

// Run `factorial` Function to find average of a list of numbers.

var num = console.log('Enter a number: ')
console.log(calcFactorial(num))
export { calcFactorial }
18 changes: 5 additions & 13 deletions Maths/Fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
5 changes: 3 additions & 2 deletions Maths/FindHcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -27,4 +27,5 @@ function findHCF (x, y) {
// When the while loop finishes the minimum of x and y is the HCF.
return Math.min(x, y)
}
console.log(findHCF(27, 36))

export { findHCF }
25 changes: 15 additions & 10 deletions Maths/FindLcm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@
'use strict'

// Find the LCM of two numbers.
function findLcm (num1, num2) {
var maxNum
var lcm
const findLcm = (num1, num2) => {
// If the input numbers are less than 1 return an error message.
if (num1 < 1 || num2 < 1) {
return 'Please enter values greater than zero.'
}

// If the input numbers are not integers return an error message.
if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) {
return 'Please enter whole numbers.'
}

let maxNum
let lcm
// Check to see whether num1 or num2 is larger.
if (num1 > num2) {
maxNum = num1
Expand All @@ -24,15 +34,10 @@ function findLcm (num1, num2) {
lcm = maxNum

while (true) {
if ((lcm % num1 === 0) && (lcm % num2 === 0)) {
break
}
if (lcm % num1 === 0 && lcm % num2 === 0) break
lcm += maxNum
}
return lcm
}

// Run `findLcm` Function
var num1 = 12
var num2 = 76
console.log(findLcm(num1, num2))
export { findLcm }
11 changes: 4 additions & 7 deletions Maths/GridGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
7 changes: 1 addition & 6 deletions Maths/MeanSquareError.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
11 changes: 1 addition & 10 deletions Maths/ModularBinaryExponentiationRecursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
9 changes: 3 additions & 6 deletions Maths/Palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @complexity: O(n)
*/

function PalindromeRecursive (string) {
const PalindromeRecursive = (string) => {
// Base case
if (string.length < 2) return true

Expand All @@ -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, '')
Expand All @@ -45,7 +45,4 @@ function PalindromeIterative (string) {
return true
}

// testing

console.log(PalindromeRecursive('Javascript Community'))
console.log(PalindromeIterative('mom'))
export { PalindromeIterative, PalindromeRecursive }
27 changes: 13 additions & 14 deletions Maths/PascalTriangle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
const numRows = 5
const addRow = (triangle) => {
const previous = triangle[triangle.length - 1]
const newRow = [1]
for (let i = 0; i < previous.length - 1; i++) {
const current = previous[i]
const next = previous[i + 1]
newRow.push(current + next)
}
newRow.push(1)
return triangle.push(newRow)
}

var generate = function (numRows) {
const generate = (numRows) => {
const triangle = [[1], [1, 1]]

if (numRows === 0) {
Expand All @@ -16,16 +26,5 @@ var generate = function (numRows) {
}
return triangle
}
var addRow = function (triangle) {
const previous = triangle[triangle.length - 1]
const newRow = [1]
for (let i = 0; i < previous.length - 1; i++) {
const current = previous[i]
const next = previous[i + 1]
newRow.push(current + next)
}
newRow.push(1)
return triangle.push(newRow)
}

generate(numRows)
export { generate }
8 changes: 2 additions & 6 deletions Maths/PiApproximationMonteCarlo.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -18,8 +18,4 @@ function piEstimation (iterations = 100000) {
return pi
}

function main () {
console.log(piEstimation())
}

main()
export { piEstimation }
49 changes: 15 additions & 34 deletions Maths/Polynomial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Polynomials are algebraic expressions consisting of two or more algebraic terms.
* Terms of a polynomial are:
Expand All @@ -20,18 +19,19 @@ class Polynomial {
* Function to construct the polynomial in terms of x using the coefficientArray
*/
construct () {
this.polynomial = this.coefficientArray.map((coefficient, exponent) => {
if (coefficient === 0) {
return '0'
}
if (exponent === 0) {
return `(${coefficient})`
} else if (exponent === 1) {
return `(${coefficient}x)`
} else {
return `(${coefficient}x^${exponent})`
}
})
this.polynomial = this.coefficientArray
.map((coefficient, exponent) => {
if (coefficient === 0) {
return '0'
}
if (exponent === 0) {
return `(${coefficient})`
} else if (exponent === 1) {
return `(${coefficient}x)`
} else {
return `(${coefficient}x^${exponent})`
}
})
.filter((x) => {
if (x !== '0') {
return x
Expand All @@ -55,28 +55,9 @@ class Polynomial {
*/
evaluate (value) {
return this.coefficientArray.reduce((result, coefficient, exponent) => {
return result + coefficient * (Math.pow(value, exponent))
return result + coefficient * Math.pow(value, exponent)
}, 0)
}
}

/**
* Function to perform tests
*/
const tests = () => {
const polynomialOne = new Polynomial([1, 2, 3, 4])
console.log('Test 1: [1,2,3,4]')
console.log('Display Polynomial ', polynomialOne.display())
// (4x^3) + (3x^2) + (2x) + (1)
console.log('Evaluate Polynomial value=2 ', polynomialOne.evaluate(2))
// 49

const polynomialTwo = new Polynomial([5, 0, 0, -4, 3])
console.log('Test 2: [5,0,0,-4,3]')
console.log('Display Polynomial ', polynomialTwo.display())
// (3x^4) + (-4x^3) + (5)
console.log('Evaluate Polynomial value=1 ', polynomialTwo.evaluate(1))
// 4
}

tests()
export { Polynomial }
Loading