-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.diff.js
More file actions
17 lines (13 loc) · 736 Bytes
/
array.diff.js
File metadata and controls
17 lines (13 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
// It should remove all values from list a, which are present in list b keeping their order.
// arrayDiff([1,2],[1]) == [2]
// If a value is present in b, all of its occurrences must be removed from the other:
// arrayDiff([1,2,2,2,3],[2]) == [1,3]
function arrayDiff(a, b) {
return a.filter((element) => {
//if the element in the a array is not found in the b array, return it to the filter function and filter it out, leaving just that element that isn't in both arrays
return b.indexOf(element) < 0;
})
}
console.log(arrayDiff([1,2], [1]))
console.log(arrayDiff([1,2,3], [1,2]))