-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareArrays.js
More file actions
35 lines (32 loc) · 1.15 KB
/
CompareArrays.js
File metadata and controls
35 lines (32 loc) · 1.15 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
/**
* This function is used to find out if one of the elements is identical in the two arrays
* @param{array} first array
* @param{array} second array
* @return{bolean} return true if any items are the same in both arrays else false
*/
function compareArrays(arr1, arr2) {
var res = [];
for (var i = 0; i < arr2.length; i++){
var check = arr1.map(function(el){
return el === arr2[i] ? true : false;
}).filter(function(el){ return el === true;})[0]
if (check){return true}
}
return false
}
/**
* Version ES6
* This function is used to find out if one of the elements is identical in the two arrays
* @param{array} first array
* @param{array} second array
* @return{bolean} return true if any items are the same in both arrays else false
*/
function compareArrays2(arr1, arr2) {
return arr1.map((el) => arr2.indexOf(el) > -1).filter((el) => el).length > 0;
}
var tab1 = ['banana', 'orange', 'cherry'];
var tab2 = ['lemon', 'banana', 'apple', 'pear'];
console.log(compareArrays(tab1, tab2)); // return true
var tab3 = ['banana', 'orange', 'cherry'];
var tab4 = ['lemon', 'apple', 'pear'];
console.log(compareArrays(tab3, tab4)); // return false