math-utils-simple 1.0.0
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 +8 -0
- package/package.json +16 -0
- package/src/add.js +3 -0
- package/src/average.js +11 -0
- package/src/divide.js +6 -0
- package/src/factorial.js +14 -0
- package/src/gcd.js +12 -0
- package/src/index.js +10 -0
- package/src/lcm.js +8 -0
- package/src/multiply.js +3 -0
- package/src/power.js +3 -0
- package/src/sqrt.js +6 -0
- package/src/subtract.js +3 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "math-utils-simple",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple math utility library for npm",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node --test"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["math", "utils", "npm", "library"],
|
|
14
|
+
"author": "Umar Farooq",
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|
package/src/add.js
ADDED
package/src/average.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default function average(numbers) {
|
|
2
|
+
if (!Array.isArray(numbers)) {
|
|
3
|
+
throw new Error("Input must be an array");
|
|
4
|
+
}
|
|
5
|
+
if (numbers.length === 0) {
|
|
6
|
+
throw new Error("Cannot calculate average of an empty array");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const sum = numbers.reduce((acc, num) => acc + num, 0);
|
|
10
|
+
return sum / numbers.length;
|
|
11
|
+
}
|
package/src/divide.js
ADDED
package/src/factorial.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default function factorial(n) {
|
|
2
|
+
if (n < 0) {
|
|
3
|
+
throw new Error("Factorial is not defined for negative numbers");
|
|
4
|
+
}
|
|
5
|
+
if (!Number.isInteger(n)) {
|
|
6
|
+
throw new Error("Factorial is only defined for integers");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let result = 1;
|
|
10
|
+
for (let i = 2; i <= n; i++) {
|
|
11
|
+
result *= i;
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}
|
package/src/gcd.js
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { default as add } from "./add.js";
|
|
2
|
+
export { default as subtract } from "./subtract.js";
|
|
3
|
+
export { default as multiply } from "./multiply.js";
|
|
4
|
+
export { default as divide } from "./divide.js";
|
|
5
|
+
export { default as power } from "./power.js";
|
|
6
|
+
export { default as sqrt } from "./sqrt.js";
|
|
7
|
+
export { default as factorial } from "./factorial.js";
|
|
8
|
+
export { default as gcd } from "./gcd.js";
|
|
9
|
+
export { default as lcm } from "./lcm.js";
|
|
10
|
+
export { default as average } from "./average.js";
|
package/src/lcm.js
ADDED
package/src/multiply.js
ADDED
package/src/power.js
ADDED
package/src/sqrt.js
ADDED
package/src/subtract.js
ADDED