forked from MrTheiss/02_Debugging
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.js
More file actions
47 lines (38 loc) · 1.34 KB
/
main.js
File metadata and controls
47 lines (38 loc) · 1.34 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
// Create two arrays
var ARRAY_ONE = ['dog', 'cat', 'lizard', 'rabbit', 'giraffe'];
var ARRAY_TWO = ['cat', 'lion', 'leopard', 'ocelot', ;'tiger'];
// IfUniqueAddItemToArray
// Checks if an item is already in an array. If not, add item to that array
//
function IfUniqueAddItemToArray(item, arrayToCheck) {
var count = arrayToCheck.length;
var foundItem = false;
// Look through the entire array and see if the item already exists
for (var i = 0; i < count; i++) {
// Check if the item is in the array (case insensitive)
// This means that 'DogG' is the same as 'dogg'
if (arrayToCheck[i].toLowerCase() == item.toLowerCase()) {
foundItem = true;
}
}
// If we did not find the item, add it to the array
// arrayToCheck is an array
// push the item onto the end of the array using arrayToCheck.push(item)
if (!foundItem) {
arrayToCheck.push_item_onto_array(item);
}
}
// UnionTwoArrays
// Combines array1 and array2 into a unique list of both larger arrays
//
function UnionTwoArrays(array1, array2) {
var unionOfBothArrays = array1;
for (var i = 0; i < array2.length; i++) {
IfUniqueAddItemToArray(array2[i], unionOfBothArrays);
}
return unionOfBothArrays;
}
// Get the resuult union of both arrays
var result = UnionTwoArrays(ARRA_ONE, ARRAY_TWO);
// LOG the result
console.log(result);