Skip to content

Commit 2b60cef

Browse files
committed
Utility Functions
1 parent b1efe72 commit 2b60cef

File tree

4 files changed

+49
-11
lines changed

4 files changed

+49
-11
lines changed

STRUCTURE.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ JS-DSA/
8080
| |-- randomized/
8181
| | |-- randomizedQuickSort.js
8282
| | |-- randomizedPrimalityTesting.js
83-
| |-- cryptography/
84-
| |-- rsaEncryption.js
85-
| |-- rsaDecryption.js
86-
| |-- aesEncryption.js
87-
| |-- aesDecryption.js
8883
|
8984
|-- utils/
9085
| |-- utilityFunctions.js
@@ -175,14 +170,9 @@ JS-DSA/
175170
| | |-- randomized/
176171
| | | |-- randomizedQuickSort.test.js
177172
| | | |-- randomizedPrimalityTesting.test.js
178-
| | |-- cryptography/
179-
| | |-- rsaEncryption.test.js
180-
| | |-- rsaDecryption.test.js
181-
| | |-- aesEncryption.test.js
182-
| | |-- aesDecryption.test.js
183173
|
184174
|-- docs/
185175
|-- data-structures.md
186176
|-- algorithms.md
187177
|-- usage-examples.md
188-
```
178+
```

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const LogisticRegression = require('./algorithms/ml-statistical/logisticRegressi
5757
const DecisionTree = require('./algorithms/ml-statistical/decisionTrees');
5858
const RandomizedQuickSort = require('./algorithms/sorting/randomizedQuickSort');
5959
const RandomizedPrimalityTesting = require('./algorithms/randomized/randomizedPrimalityTesting');
60+
const UtilityFunctions = require('./utils/utilityFunctions');
6061
// Import other data structures and algorithms as needed
6162

6263
module.exports = {
@@ -120,5 +121,6 @@ module.exports = {
120121
DecisionTree,
121122
RandomizedQuickSort,
122123
RandomizedPrimalityTesting,
124+
UtilityFunctions,
123125
// Export other data structures and algorithms as well
124126
};

test/utils/utilityFunctions.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const UtilityFunctions = require('../../utils/utilityFunctions');
2+
3+
describe('UtilityFunctions', () => {
4+
describe('sum', () => {
5+
it('should return the sum of an array of numbers', () => {
6+
const numbers = [1, 2, 3, 4, 5];
7+
const result = UtilityFunctions.sum(numbers);
8+
expect(result).toBe(15);
9+
});
10+
11+
it('should handle an empty array', () => {
12+
const numbers = [];
13+
const result = UtilityFunctions.sum(numbers);
14+
expect(result).toBe(0);
15+
});
16+
});
17+
18+
describe('average', () => {
19+
it('should return the average of an array of numbers', () => {
20+
const numbers = [2, 4, 6, 8, 10];
21+
const result = UtilityFunctions.average(numbers);
22+
expect(result).toBe(6);
23+
});
24+
25+
it('should handle an empty array', () => {
26+
const numbers = [];
27+
const result = UtilityFunctions.average(numbers);
28+
expect(result).toBe(0);
29+
});
30+
});
31+
});

utils/utilityFunctions.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class UtilityFunctions {
2+
static sum(arr) {
3+
return arr.reduce((acc, current) => acc + current, 0);
4+
}
5+
6+
static average(arr) {
7+
if (arr.length === 0) {
8+
return 0;
9+
}
10+
const total = this.sum(arr);
11+
return total / arr.length;
12+
}
13+
}
14+
15+
module.exports = UtilityFunctions;

0 commit comments

Comments
 (0)