From 1f13f09bda972c5524430acd0927160e7d56e482 Mon Sep 17 00:00:00 2001 From: Justin Zou Date: 2024年7月13日 18:23:40 -0400 Subject: [PATCH 1/6] feat: Added Standard Deviation algorithm for Math --- Maths/StandardDeviation.js | 32 +++++++++++++++++++++++++ Maths/test/StandardDeviation.test.js | 36 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 Maths/StandardDeviation.js create mode 100644 Maths/test/StandardDeviation.test.js diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js new file mode 100644 index 0000000000..701a5d8de8 --- /dev/null +++ b/Maths/StandardDeviation.js @@ -0,0 +1,32 @@ +/* + * Returns the standard deviation given an array of integers + * @param int[] nums array of integers + * @return int standard deviation + * @see https://en.wikipedia.org/wiki/Standard_deviation + */ +function standardDeviation(nums) { + if (nums.length < 2) { + throw new Error('At least two numbers in input array are required!') + } + if (!Array.isArray(nums)) { + throw new TypeError('Array required!') + } + + let sum = 0 + for (let i = 0; i < nums.length; i++) { + if (!Number.isInteger(nums[i])) { + throw new TypeError('Integer type required in array input!') + } + sum += nums[i] + } + let avg = sum / nums.length + let deviationSquareSum = 0 + for (let i = 0; i < nums.length; i++) { + let square = Math.pow(nums[i] - avg, 2) + deviationSquareSum += square + } + + return Math.sqrt(deviationSquareSum / nums.length) +} + +export { standardDeviation } diff --git a/Maths/test/StandardDeviation.test.js b/Maths/test/StandardDeviation.test.js new file mode 100644 index 0000000000..f2b3b318bd --- /dev/null +++ b/Maths/test/StandardDeviation.test.js @@ -0,0 +1,36 @@ +import { standardDeviation } from '../StandardDeviation.js' + +test('Calculate STD of 3,5,6,10,23,12,19', () => { + expect(standardDeviation([3, 5, 6, 10, 23, 12, 19])).toBeCloseTo( + 6.9164105353773 + ) +}) + +test('Calculate STD of -2,-5,1,12,23,-81,-23', () => { + expect(standardDeviation([-2, -5, 1, 12, 23, -81, -23])).toBeCloseTo( + 31.598889156399 + ) +}) + +test('Calculate STD of 0,0,0', () => { + expect(standardDeviation([0, 0, 0])).toBeCloseTo(0) +}) + +test('Calculate STD of -7,7', () => { + expect(standardDeviation([-7, 7])).toBeCloseTo(7) +}) + +test('Throw error - array has less than two items', () => { + expect(() => standardDeviation([])).toThrow() + expect(() => standardDeviation([7])).toThrow() +}) + +test('Throw type error - not an array', () => { + expect(() => standardDeviation(2)).toThrow() + expect(() => standardDeviation('not an array')).toThrow() +}) + +test('Throw type error - not a number inside array', () => { + expect(() => standardDeviation([5, 'not a number', 2])).toThrow() + expect(() => standardDeviation([1, [2], 3])).toThrow() +}) From e60007bedfe129026db08818f4a82141db401146 Mon Sep 17 00:00:00 2001 From: justclaner <111201418+justclaner@users.noreply.github.com> Date: 2024年7月13日 18:41:34 -0400 Subject: [PATCH 2/6] feat: Added Standard Deviation algorithm for Math --- Maths/StandardDeviation.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js index 701a5d8de8..03a297619c 100644 --- a/Maths/StandardDeviation.js +++ b/Maths/StandardDeviation.js @@ -1,7 +1,7 @@ /* * Returns the standard deviation given an array of integers - * @param int[] nums array of integers - * @return int standard deviation + * @param number[] nums array of integers + * @return number standard deviation * @see https://en.wikipedia.org/wiki/Standard_deviation */ function standardDeviation(nums) { @@ -14,8 +14,8 @@ function standardDeviation(nums) { let sum = 0 for (let i = 0; i < nums.length; i++) { - if (!Number.isInteger(nums[i])) { - throw new TypeError('Integer type required in array input!') + if (isNaN(nums[i])) { + throw new TypeError('Number type required in array input!') } sum += nums[i] } From be42d2530960033f3c87631b71e6cb456ec2c8d2 Mon Sep 17 00:00:00 2001 From: Justin Zou Date: 2024年7月13日 18:51:34 -0400 Subject: [PATCH 3/6] feat: Added Standard Deviation algorithm in Math --- Maths/StandardDeviation.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js index 701a5d8de8..03a297619c 100644 --- a/Maths/StandardDeviation.js +++ b/Maths/StandardDeviation.js @@ -1,7 +1,7 @@ /* * Returns the standard deviation given an array of integers - * @param int[] nums array of integers - * @return int standard deviation + * @param number[] nums array of integers + * @return number standard deviation * @see https://en.wikipedia.org/wiki/Standard_deviation */ function standardDeviation(nums) { @@ -14,8 +14,8 @@ function standardDeviation(nums) { let sum = 0 for (let i = 0; i < nums.length; i++) { - if (!Number.isInteger(nums[i])) { - throw new TypeError('Integer type required in array input!') + if (isNaN(nums[i])) { + throw new TypeError('Number type required in array input!') } sum += nums[i] } From f5dbdcea838e370ae43f5acf99a1bf611238eb9b Mon Sep 17 00:00:00 2001 From: Justin Zou Date: 2024年7月13日 18:56:53 -0400 Subject: [PATCH 4/6] feat: Added Standard Deviation algorithm in Math --- Maths/StandardDeviation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js index 03a297619c..4223fd2732 100644 --- a/Maths/StandardDeviation.js +++ b/Maths/StandardDeviation.js @@ -14,7 +14,7 @@ function standardDeviation(nums) { let sum = 0 for (let i = 0; i < nums.length; i++) { - if (isNaN(nums[i])) { + if (isNaN(nums[i]) || Array.isArray(nums[i])) { throw new TypeError('Number type required in array input!') } sum += nums[i] From f842ef9d780d1685b1cb09acd1b681ae374c58fe Mon Sep 17 00:00:00 2001 From: Justin Zou Date: 2024年7月13日 19:02:18 -0400 Subject: [PATCH 5/6] feat: Added Standard Deviation algorithm to Math --- Maths/StandardDeviation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js index 4223fd2732..962a2fc40f 100644 --- a/Maths/StandardDeviation.js +++ b/Maths/StandardDeviation.js @@ -14,7 +14,7 @@ function standardDeviation(nums) { let sum = 0 for (let i = 0; i < nums.length; i++) { - if (isNaN(nums[i]) || Array.isArray(nums[i])) { + if (typeof (nums[i]) != 'number') { throw new TypeError('Number type required in array input!') } sum += nums[i] From c6892617f28c419b3b85ccec225fd9daf5781c44 Mon Sep 17 00:00:00 2001 From: Justin Zou Date: 2024年7月13日 19:07:10 -0400 Subject: [PATCH 6/6] feat: Added Standard Deviation algorithm to Math --- Maths/StandardDeviation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/StandardDeviation.js b/Maths/StandardDeviation.js index 962a2fc40f..2ad21d33da 100644 --- a/Maths/StandardDeviation.js +++ b/Maths/StandardDeviation.js @@ -14,7 +14,7 @@ function standardDeviation(nums) { let sum = 0 for (let i = 0; i < nums.length; i++) { - if (typeof (nums[i]) != 'number') { + if (typeof nums[i] != 'number') { throw new TypeError('Number type required in array input!') } sum += nums[i]

AltStyle によって変換されたページ (->オリジナル) /