mohamedourmath 1.8.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/index.js +61 -0
- package/package.json +12 -0
- package/test.js +123 -0
package/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
|
|
2
|
+
const ourMath = require('./test.js')
|
|
3
|
+
|
|
4
|
+
let sum = ourMath.unionTwoSets([1,2,3], [3,4,5]);
|
|
5
|
+
console.log(sum);
|
|
6
|
+
if(sum[0] == 1 &&
|
|
7
|
+
sum[1] == 2 &&
|
|
8
|
+
sum[2] == 3 &&
|
|
9
|
+
sum[3] == 4 &&
|
|
10
|
+
sum[4] == 5
|
|
11
|
+
){
|
|
12
|
+
console.log("unionTwoSets success")
|
|
13
|
+
}else{
|
|
14
|
+
console.log("unionTwoSets failure")
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
let intersection = ourMath.intersection([1,2,3,4],[2,4,6,7]);
|
|
19
|
+
console.log(intersection);
|
|
20
|
+
|
|
21
|
+
if(intersection[0] === 2 &&
|
|
22
|
+
intersection[1] === 4
|
|
23
|
+
){
|
|
24
|
+
console.log("intersection success")
|
|
25
|
+
}else{
|
|
26
|
+
console.log("intersection failure")
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
let Max = ourMath.Max(4,5,6,2,4,6,8,9,1,0);
|
|
31
|
+
console.log(Max);
|
|
32
|
+
|
|
33
|
+
if(Max == 9){
|
|
34
|
+
console.log("Max success")
|
|
35
|
+
}else{
|
|
36
|
+
console.log("Max failure")
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
let min = ourMath.Min(2, 55, .233, 44, 3.4, 534);
|
|
40
|
+
console.log(min);
|
|
41
|
+
if(min == .233){
|
|
42
|
+
console.log("Min success");
|
|
43
|
+
}else{
|
|
44
|
+
console.log("Min failure");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let dif = ourMath.setsDifference([1, 2, 3], [2, 3, 4]);
|
|
48
|
+
console.log(dif);
|
|
49
|
+
if(dif[0] == [1] && dif[1] == [4]){
|
|
50
|
+
console.log("sets difference success");
|
|
51
|
+
}else {
|
|
52
|
+
console.log("sets difference failure");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let avg = ourMath.avg(1,2,3,4,5);
|
|
56
|
+
console.log(avg);
|
|
57
|
+
if(avg == 3){
|
|
58
|
+
console.log("avg success");
|
|
59
|
+
}else {
|
|
60
|
+
console.log("avg failure");
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mohamedourmath",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"description": "a few math functions",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "random",
|
|
7
|
+
"type": "commonjs",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
}
|
|
12
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/*
|
|
2
|
+
union two sets function.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
exports.unionTwoSets= function unionTwoSets(first_set, second_set){
|
|
6
|
+
|
|
7
|
+
let result = [];
|
|
8
|
+
|
|
9
|
+
for(let i = 0; i<first_set.length; i++){
|
|
10
|
+
result.push(first_set[i]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
for (let i =0; i<second_set.length; i++){
|
|
14
|
+
if(!result.includes(second_set[i])){
|
|
15
|
+
result.push(second_set[i]);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/*
|
|
22
|
+
intersection function.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
exports.intersection = function intersection(first_set, second_set){
|
|
26
|
+
let result =[];
|
|
27
|
+
for(let i =0; i<first_set.length; i++){
|
|
28
|
+
if(second_set.includes(first_set[i])){
|
|
29
|
+
result.push(first_set[i]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
Max number
|
|
37
|
+
|
|
38
|
+
inputs: ...nums
|
|
39
|
+
steps:
|
|
40
|
+
temp variable max = the smallest number value
|
|
41
|
+
-for each item in nums
|
|
42
|
+
- if item > max then max = num
|
|
43
|
+
return max
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
exports.Max = function max(...nums){
|
|
47
|
+
let temp = Number.NEGATIVE_INFINITY;
|
|
48
|
+
for(let i=0; i<nums.length; i++){
|
|
49
|
+
if(nums[i]>temp){
|
|
50
|
+
temp = nums[i];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return temp;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/*
|
|
57
|
+
Min number
|
|
58
|
+
|
|
59
|
+
inputs: ...nums rest parameter
|
|
60
|
+
steps:
|
|
61
|
+
temp variable = positive_infinity
|
|
62
|
+
for each item in nums
|
|
63
|
+
- if num < temp then temp = num.
|
|
64
|
+
return temp
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
exports.Min = function Min(...nums){
|
|
68
|
+
let temp = Number.POSITIVE_INFINITY;
|
|
69
|
+
for(let i=0; i<nums.length; i++){
|
|
70
|
+
if(nums[i]< temp){
|
|
71
|
+
temp = nums[i];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return temp;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
Difference sets
|
|
79
|
+
|
|
80
|
+
- Inputs : 2 arrays .. first_set .. second_set
|
|
81
|
+
- Steps:
|
|
82
|
+
temp variable = []
|
|
83
|
+
for each item in first set not in the second set
|
|
84
|
+
- push to temp
|
|
85
|
+
for each item in second set not in the first set
|
|
86
|
+
push to temp
|
|
87
|
+
return temp
|
|
88
|
+
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
exports.setsDifference = function setsDifference(first_set, second_set){
|
|
92
|
+
let temp = [];
|
|
93
|
+
for(let i=0; i<first_set.length; i++ ){
|
|
94
|
+
if(!second_set.includes(first_set[i])){
|
|
95
|
+
temp.push(first_set[i]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for(let i=0; i<second_set.length; i++){
|
|
100
|
+
if(!first_set.includes(second_set[i])){
|
|
101
|
+
temp.push(second_set[i]);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return temp;
|
|
105
|
+
}
|
|
106
|
+
/*
|
|
107
|
+
avg function
|
|
108
|
+
|
|
109
|
+
-inputs: nums[] ...nums
|
|
110
|
+
-steps:
|
|
111
|
+
sum all the values
|
|
112
|
+
divide the sum on the lenght of the array
|
|
113
|
+
return result
|
|
114
|
+
*/
|
|
115
|
+
|
|
116
|
+
exports.avg = function avg(...nums){
|
|
117
|
+
let result=0;
|
|
118
|
+
for(let i=0; i<nums.length; i++){
|
|
119
|
+
result += nums[i];
|
|
120
|
+
}
|
|
121
|
+
result /= nums.length;
|
|
122
|
+
return result;
|
|
123
|
+
}
|