-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (92 loc) · 2.92 KB
/
index.js
File metadata and controls
113 lines (92 loc) · 2.92 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(function (mergeArrays) {
'use strict';
if (typeof mergeArrays !== 'function') {
console.warn('You should import "arrays/mergeArrays.js" in order to use mergeArrays module!');
return;
}
var arrayA = [],
arrayA2 = [],
arrayB,
arrayB2 = [],
i = 100,
len = 2000,
arrayC,
arrayC2 = [],
lengthMatch = true,
wellSorted = true,
arrayAIsInResult = true,
arrayBIsInResult = true;
for (; i <= len; i += 1) {
arrayA.push(i * 2);
}
arrayB = [541, 7009, 1001, 501, 1101, 3110, 4311, 9141, 701, 99, 451, 9, 3511, 971];
//make a clone of arrayA and arrayB
for (i = 0, len = arrayA.length; i < len; i += 1) {
arrayA2.push(arrayA[i]);
}
for (i = 0, len = arrayB.length; i < len; i += 1) {
arrayB2.push(arrayB[i]);
}
arrayC = mergeArrays(arrayA, arrayB);
//restore the original values
arrayA = arrayA2;
arrayB = arrayB2;
console.log('arrayA.length:' + arrayA.length);
console.log('arrayB.length:' + arrayB.length);
console.log('arrayA.length + arrayB.length:' + (arrayA.length + arrayB.length));
console.log('arrayC.length:' + arrayC.length);
lengthMatch = (arrayA.length + arrayB.length === arrayC.length);
if (!lengthMatch) {
console.log('mergeArrays module is not merging properly the length missmatch!');
}
for (i = 0; i < arrayA.length; i += 1) {
if (arrayC.indexOf(arrayA[i]) === -1) {
arrayAIsInResult = false;
break;
}
}
if (arrayAIsInResult) {
console.log('mergeArrays result(arrayC) contains all of the "arrayA"\'s items');
} else {
console.log('mergeArrays result(arrayC) does not contain all of the "arrayA"\'s items');
}
for (i = 0; i < arrayB.length; i += 1) {
if (arrayC.indexOf(arrayB[i]) === -1) {
arrayBIsInResult = false;
break;
}
}
if (arrayBIsInResult) {
console.log('mergeArrays result(arrayC) contains all of the "arrayB"\'s items');
} else {
console.log('mergeArrays result(arrayC) does not contain all of the "arrayB"\'s items');
}
//make a clone to check if the result is well sorted
for (i = 0, len = arrayC.length; i < len; i += 1) {
arrayC2.push(arrayC[i]);
}
arrayC2.sort(function (x, y) {
return x - y;
});
//compare arrayC2 and arrayC
for (i = 0, len = arrayC.length; i < len; i += 1) {
if (arrayC[i] !== arrayC2[i]) {
wellSorted = false;
break;
}
}
if (wellSorted) {
console.log('mergeArrays result(arrayC) is well-sorted');
} else {
console.log('mergeArrays result(arrayC) is not well-sorted');
}
if (lengthMatch && arrayAIsInResult && arrayBIsInResult && wellSorted) {
console.log('*********GREAT*JOB***********');
console.log('mergeArrays module IS WORKING!');
console.log('*********TEST*FINISHED*******');
} else {
console.log('++++++++++++FIX IT!+++++++');
console.log('mergeArrays IS NOT WORKING!');
console.log('+++++++++TEST+FINISHED+++++');
}
}(this.mergeArrays));