averagejs 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,12 +6,33 @@ A JavaScript library that can calculate the average such as mean, median, mode f
6
6
 
7
7
  Add this script tag to your HTML code
8
8
 
9
+ ```html
10
+ <script src="https://cdn.jsdelivr.net/npm/averagejs@1.0.1/script.min.js"></script>
11
+ ```
12
+
9
13
  # Useage
10
14
 
11
15
  **Get the Mean of a bunch of numbers.**
12
16
 
13
17
  ```JavaScript
14
- let inputNumbers = [2, "9", 4, 6, 8];
18
+ let inputNumbers = [2, 9, 4, 6, 8];
15
19
  let result = getMean(inputNumbers);
16
20
  console.log(result); // 5
17
21
  ```
22
+
23
+ ----
24
+ **Get the Median of a bunch of numbers.**
25
+
26
+ ```JavaScript
27
+ let inputNumbers = [5, 4, 2, 6, 3];
28
+ let result = getMean(inputNumbers);
29
+ console.log(result); // 4
30
+ ```
31
+
32
+ **Get the Mode of a bunch of numbers.**
33
+
34
+ ```JavaScript
35
+ let inputNumbers = [1, 1, 2, 2, 3];
36
+ let result = getModes(inputNumbers);
37
+ console.log(result); // 1,2
38
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "averagejs",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "A JavaScript library that can calculate the average such as mean, median, mode from a bunch of numbers.",
5
5
  "keywords": [
6
6
  "average",
package/script.js CHANGED
@@ -41,6 +41,21 @@ function getMedian(inputList) {
41
41
  }
42
42
  }
43
43
 
44
- function getMode(inputList) {
44
+ function getModes(inputList) {
45
+ const count = {};
45
46
 
47
+ for (let num of inputList) {
48
+ count[num] = (count[num] || 0) + 1;
49
+ }
50
+
51
+ let maxCount = Math.max(...Object.values(count));
52
+ let modes = [];
53
+
54
+ for (let num in count) {
55
+ if (count[num] === maxCount) {
56
+ modes.push(Number(num));
57
+ }
58
+ }
59
+
60
+ return modes;
46
61
  }
package/test.html CHANGED
@@ -1,4 +1,6 @@
1
1
  <script src="script.js"></script>
2
2
  <script>
3
- console.log(getMedian([4, 6, 5]))
3
+ let inputNumbers = [1, 1, 2, 2, 3];
4
+ let result = getModes(inputNumbers);
5
+ console.log(result);
4
6
  </script>