-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
84 lines (73 loc) · 1.95 KB
/
array.js
File metadata and controls
84 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var exports = module.exports = {};
//CHECKS IF THE ARE THE EXACT SAME IN TERMS OF DATA
exports.equal = function(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
if (arr1[i] !== arr2[j]) {
return false;
}
}
}
return true;
}
//CHECKS IF NOT EMPTY, NO UNDEFINED OR NULL VALUES, AND IF AN ARRAY
exports.validate = function(arr) {
var pass = true;
try {
if (!(arr instanceof Array)) {
throw "Error, Coordinates must be in an array.";
}
if (arr.length == 0) {
throw "Error, Coordinates must be non-empty."
}
for (let coord of arr) {
if (coord == undefined || coord == null) {
throw "Error,Coordinate values must not be null or undefined."
}
}
} catch (err) {
console.log(err);
pass = false;
}
if (pass) {
// console.log('this array is validated');
return true
} else { return false}
}
//CHECKS IF THE SAME LENGTH
exports.sameLength = function(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
} else {
return true;
}
}
//given at least one arrays, or many all within one array, must be 2D
exports.arrayOfAllArraySizes = function(args) {
return args.reduce((acc, arr) => {return acc.concat(arr.length)},[]);
}
//must be called on the array given by multiArraySizes
exports.areAllSameSize = function(array){
return array.every(function(element, index, array) {
return element == array[0];
});
}
//given a 2d array of arrays, will return the largest array
exports.largestArray = function(args){
return args.reduce((acc, arr) => {return (arr.length > acc.length) ? arr : acc});
}
exports.flattenArray = function(array) {
return array.reduce( (a, b) => {return a.concat(b);},[]);
}
exports.sameInside = function(array) {
// return array.every((current,index,array) => {return current == array[0];});
for (var i = 0; i < array.length; i++) {
if (array[0] !== array[i]) {
return false;
}
}
return true;
}